string_list: add a new function, filter_string_list()
[git/jrn.git] / string-list.h
blob7d18e622ecab924c0ad8e351a1fcf01287104d90
1 #ifndef STRING_LIST_H
2 #define STRING_LIST_H
4 struct string_list_item {
5 char *string;
6 void *util;
7 };
8 struct string_list {
9 struct string_list_item *items;
10 unsigned int nr, alloc;
11 unsigned int strdup_strings:1;
14 #define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0 }
15 #define STRING_LIST_INIT_DUP { NULL, 0, 0, 1 }
17 void print_string_list(const struct string_list *p, const char *text);
18 void string_list_clear(struct string_list *list, int free_util);
20 /* Use this function to call a custom clear function on each util pointer */
21 /* The string associated with the util pointer is passed as the second argument */
22 typedef void (*string_list_clear_func_t)(void *p, const char *str);
23 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);
25 /* Use this function or the macro below to iterate over each item */
26 typedef int (*string_list_each_func_t)(struct string_list_item *, void *);
27 int for_each_string_list(struct string_list *list,
28 string_list_each_func_t, void *cb_data);
29 #define for_each_string_list_item(item,list) \
30 for (item = (list)->items; item < (list)->items + (list)->nr; ++item)
33 * Apply want to each item in list, retaining only the ones for which
34 * the function returns true. If free_util is true, call free() on
35 * the util members of any items that have to be deleted. Preserve
36 * the order of the items that are retained.
38 void filter_string_list(struct string_list *list, int free_util,
39 string_list_each_func_t want, void *cb_data);
42 /* Use these functions only on sorted lists: */
43 int string_list_has_string(const struct string_list *list, const char *string);
44 int string_list_find_insert_index(const struct string_list *list, const char *string,
45 int negative_existing_index);
46 struct string_list_item *string_list_insert(struct string_list *list, const char *string);
47 struct string_list_item *string_list_insert_at_index(struct string_list *list,
48 int insert_at, const char *string);
49 struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
52 /* Use these functions only on unsorted lists: */
55 * Add string to the end of list. If list->strdup_string is set, then
56 * string is copied; otherwise the new string_list_entry refers to the
57 * input string.
59 struct string_list_item *string_list_append(struct string_list *list, const char *string);
62 * Like string_list_append(), except string is never copied. When
63 * list->strdup_strings is set, this function can be used to hand
64 * ownership of a malloc()ed string to list without making an extra
65 * copy.
67 struct string_list_item *string_list_append_nodup(struct string_list *list, char *string);
69 void sort_string_list(struct string_list *list);
70 int unsorted_string_list_has_string(struct string_list *list, const char *string);
71 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
72 const char *string);
74 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
77 * Split string into substrings on character delim and append the
78 * substrings to list. The input string is not modified.
79 * list->strdup_strings must be set, as new memory needs to be
80 * allocated to hold the substrings. If maxsplit is non-negative,
81 * then split at most maxsplit times. Return the number of substrings
82 * appended to list.
84 * Examples:
85 * string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"]
86 * string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"]
87 * string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"]
88 * string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""]
89 * string_list_split(l, "", ':', -1) -> [""]
90 * string_list_split(l, ":", ':', -1) -> ["", ""]
92 int string_list_split(struct string_list *list, const char *string,
93 int delim, int maxsplit);
96 * Like string_list_split(), except that string is split in-place: the
97 * delimiter characters in string are overwritten with NULs, and the
98 * new string_list_items point into string (which therefore must not
99 * be modified or freed while the string_list is in use).
100 * list->strdup_strings must *not* be set.
102 int string_list_split_in_place(struct string_list *list, char *string,
103 int delim, int maxsplit);
104 #endif /* STRING_LIST_H */