blob: 29bfb7ae45931686e14c91b1a47a703e7b32fe52 [file] [log] [blame]
Thiago Farina0afcb5f2010-01-08 17:45:08 -05001#ifndef STRING_LIST_H
2#define STRING_LIST_H
Johannes Schindelinc455c872008-07-21 19:03:49 +01003
4struct string_list_item {
5 char *string;
6 void *util;
7};
Junio C Hamano8dd5afc2013-01-07 12:24:55 -08008
9typedef int (*compare_strings_fn)(const char *, const char *);
10
Jonathan Nieder9cba13c2011-03-16 02:08:34 -050011struct string_list {
Johannes Schindelinc455c872008-07-21 19:03:49 +010012 struct string_list_item *items;
13 unsigned int nr, alloc;
14 unsigned int strdup_strings:1;
Junio C Hamano8dd5afc2013-01-07 12:24:55 -080015 compare_strings_fn cmp; /* NULL uses strcmp() */
Johannes Schindelinc455c872008-07-21 19:03:49 +010016};
17
Tanay Abhraacb3d222014-06-03 02:13:18 -070018#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0, NULL }
19#define STRING_LIST_INIT_DUP { NULL, 0, 0, 1, NULL }
Thiago Farina183113a2010-07-04 16:46:19 -030020
Tanay Abhra3ed3f5f2014-07-18 02:18:59 -070021void string_list_init(struct string_list *list, int strdup_strings);
22
Julian Phillipscb944f62010-06-26 00:41:33 +010023void print_string_list(const struct string_list *p, const char *text);
Johannes Schindelinc455c872008-07-21 19:03:49 +010024void string_list_clear(struct string_list *list, int free_util);
25
Marius Storm-Olsencfa1ee62009-02-08 15:34:28 +010026/* Use this function to call a custom clear function on each util pointer */
27/* The string associated with the util pointer is passed as the second argument */
28typedef void (*string_list_clear_func_t)(void *p, const char *str);
29void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);
30
Alex Riesen8d316352010-07-03 14:40:04 +020031/* Use this function or the macro below to iterate over each item */
Jay Soffianc6f5a7a2009-02-25 03:32:18 -050032typedef int (*string_list_each_func_t)(struct string_list_item *, void *);
Julian Phillipsb684e972010-06-26 00:41:34 +010033int for_each_string_list(struct string_list *list,
34 string_list_each_func_t, void *cb_data);
Alex Riesen8d316352010-07-03 14:40:04 +020035#define for_each_string_list_item(item,list) \
36 for (item = (list)->items; item < (list)->items + (list)->nr; ++item)
Jay Soffianc6f5a7a2009-02-25 03:32:18 -050037
Michael Haggertyeb5f0c72012-09-12 16:04:44 +020038/*
39 * Apply want to each item in list, retaining only the ones for which
40 * the function returns true. If free_util is true, call free() on
41 * the util members of any items that have to be deleted. Preserve
42 * the order of the items that are retained.
43 */
44void filter_string_list(struct string_list *list, int free_util,
45 string_list_each_func_t want, void *cb_data);
46
Michael Haggertyf103f952012-09-12 16:04:46 +020047/*
Michael Haggerty6bb2a132012-11-04 08:07:06 +010048 * Remove any empty strings from the list. If free_util is true, call
49 * free() on the util members of any items that have to be deleted.
50 * Preserve the order of the items that are retained.
51 */
52void string_list_remove_empty_items(struct string_list *list, int free_util);
53
Johannes Schindelinc455c872008-07-21 19:03:49 +010054/* Use these functions only on sorted lists: */
55int string_list_has_string(const struct string_list *list, const char *string);
Marius Storm-Olsencfa1ee62009-02-08 15:34:28 +010056int string_list_find_insert_index(const struct string_list *list, const char *string,
57 int negative_existing_index);
Stefan Bellerfc665052014-11-24 13:22:02 -080058/*
59 * Inserts the given string into the sorted list.
60 * If the string already exists, the list is not altered.
61 * Returns the string_list_item, the string is part of.
62 */
Julian Phillips78a395d2010-06-26 00:41:35 +010063struct string_list_item *string_list_insert(struct string_list *list, const char *string);
Stefan Bellerfc665052014-11-24 13:22:02 -080064
65/*
Brandon Williams3a300332017-04-19 16:13:21 -070066 * Removes the given string from the sorted list.
67 * If the string doesn't exist, the list is not altered.
68 */
69extern void string_list_remove(struct string_list *list, const char *string,
70 int free_util);
71
72/*
Stefan Bellerfc665052014-11-24 13:22:02 -080073 * Checks if the given string is part of a sorted list. If it is part of the list,
74 * return the coresponding string_list_item, NULL otherwise.
75 */
Julian Phillipse8c8b712010-06-26 00:41:37 +010076struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
Johannes Schindelinc455c872008-07-21 19:03:49 +010077
Michael Haggerty31d54512012-09-12 16:04:45 +020078/*
79 * Remove all but the first of consecutive entries with the same
80 * string value. If free_util is true, call free() on the util
81 * members of any items that have to be deleted.
82 */
83void string_list_remove_duplicates(struct string_list *sorted_list, int free_util);
84
Michael Haggertye448fed2012-09-12 16:04:42 +020085
Johannes Schindelinc455c872008-07-21 19:03:49 +010086/* Use these functions only on unsorted lists: */
Michael Haggertye448fed2012-09-12 16:04:42 +020087
88/*
89 * Add string to the end of list. If list->strdup_string is set, then
90 * string is copied; otherwise the new string_list_entry refers to the
91 * input string.
92 */
Julian Phillips1d2f80f2010-06-26 00:41:38 +010093struct string_list_item *string_list_append(struct string_list *list, const char *string);
Michael Haggertye448fed2012-09-12 16:04:42 +020094
95/*
96 * Like string_list_append(), except string is never copied. When
97 * list->strdup_strings is set, this function can be used to hand
98 * ownership of a malloc()ed string to list without making an extra
99 * copy.
100 */
101struct string_list_item *string_list_append_nodup(struct string_list *list, char *string);
102
Michael Haggerty3383e192014-11-25 09:02:35 +0100103void string_list_sort(struct string_list *list);
Johannes Schindelinc455c872008-07-21 19:03:49 +0100104int unsorted_string_list_has_string(struct string_list *list, const char *string);
Stephen Boyde2421482010-03-24 00:16:02 -0700105struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
106 const char *string);
Michael Haggertye448fed2012-09-12 16:04:42 +0200107
Johannes Sixt86d4b522011-08-11 23:20:00 -0600108void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
Michael Haggertyff919f92012-09-12 16:04:43 +0200109
110/*
111 * Split string into substrings on character delim and append the
112 * substrings to list. The input string is not modified.
113 * list->strdup_strings must be set, as new memory needs to be
114 * allocated to hold the substrings. If maxsplit is non-negative,
115 * then split at most maxsplit times. Return the number of substrings
116 * appended to list.
117 *
118 * Examples:
119 * string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"]
120 * string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"]
121 * string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"]
122 * string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""]
123 * string_list_split(l, "", ':', -1) -> [""]
124 * string_list_split(l, ":", ':', -1) -> ["", ""]
125 */
126int string_list_split(struct string_list *list, const char *string,
127 int delim, int maxsplit);
128
129/*
130 * Like string_list_split(), except that string is split in-place: the
131 * delimiter characters in string are overwritten with NULs, and the
132 * new string_list_items point into string (which therefore must not
133 * be modified or freed while the string_list is in use).
134 * list->strdup_strings must *not* be set.
135 */
136int string_list_split_in_place(struct string_list *list, char *string,
137 int delim, int maxsplit);
Thiago Farina0afcb5f2010-01-08 17:45:08 -0500138#endif /* STRING_LIST_H */