The ninth batch for 2.15
[git/git.git] / string-list.h
CommitLineData
0afcb5f7
TF
1#ifndef STRING_LIST_H
2#define STRING_LIST_H
c455c87c
JS
3
4struct string_list_item {
5 char *string;
6 void *util;
7};
8dd5afc9
JH
8
9typedef int (*compare_strings_fn)(const char *, const char *);
10
9cba13ca 11struct string_list {
c455c87c
JS
12 struct string_list_item *items;
13 unsigned int nr, alloc;
14 unsigned int strdup_strings:1;
8dd5afc9 15 compare_strings_fn cmp; /* NULL uses strcmp() */
c455c87c
JS
16};
17
acb3d222
TA
18#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0, NULL }
19#define STRING_LIST_INIT_DUP { NULL, 0, 0, 1, NULL }
183113a5 20
3ed3f5fe
TA
21void string_list_init(struct string_list *list, int strdup_strings);
22
cb944f6b 23void print_string_list(const struct string_list *p, const char *text);
c455c87c
JS
24void string_list_clear(struct string_list *list, int free_util);
25
cfa1ee6b
MSO
26/* 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
8d31635c 31/* Use this function or the macro below to iterate over each item */
c6f5a7a9 32typedef int (*string_list_each_func_t)(struct string_list_item *, void *);
b684e977
JP
33int for_each_string_list(struct string_list *list,
34 string_list_each_func_t, void *cb_data);
ac7da78e
MH
35#define for_each_string_list_item(item,list) \
36 for (item = (list)->items; \
37 item && item < (list)->items + (list)->nr; \
38 ++item)
c6f5a7a9 39
eb5f0c7a
MH
40/*
41 * Apply want to each item in list, retaining only the ones for which
42 * the function returns true. If free_util is true, call free() on
43 * the util members of any items that have to be deleted. Preserve
44 * the order of the items that are retained.
45 */
46void filter_string_list(struct string_list *list, int free_util,
47 string_list_each_func_t want, void *cb_data);
48
6bb2a137
MH
49/*
50 * Remove any empty strings from the list. If free_util is true, call
51 * free() on the util members of any items that have to be deleted.
52 * Preserve the order of the items that are retained.
53 */
54void string_list_remove_empty_items(struct string_list *list, int free_util);
55
c455c87c
JS
56/* Use these functions only on sorted lists: */
57int string_list_has_string(const struct string_list *list, const char *string);
cfa1ee6b
MSO
58int string_list_find_insert_index(const struct string_list *list, const char *string,
59 int negative_existing_index);
fc66505c
SB
60/*
61 * Inserts the given string into the sorted list.
62 * If the string already exists, the list is not altered.
63 * Returns the string_list_item, the string is part of.
64 */
78a395d3 65struct string_list_item *string_list_insert(struct string_list *list, const char *string);
fc66505c 66
3a300333
BW
67/*
68 * Removes the given string from the sorted list.
69 * If the string doesn't exist, the list is not altered.
70 */
71extern void string_list_remove(struct string_list *list, const char *string,
72 int free_util);
73
fc66505c
SB
74/*
75 * Checks if the given string is part of a sorted list. If it is part of the list,
76 * return the coresponding string_list_item, NULL otherwise.
77 */
e8c8b713 78struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
c455c87c 79
31d5451e
MH
80/*
81 * Remove all but the first of consecutive entries with the same
82 * string value. If free_util is true, call free() on the util
83 * members of any items that have to be deleted.
84 */
85void string_list_remove_duplicates(struct string_list *sorted_list, int free_util);
86
e448fed8 87
c455c87c 88/* Use these functions only on unsorted lists: */
e448fed8
MH
89
90/*
91 * Add string to the end of list. If list->strdup_string is set, then
92 * string is copied; otherwise the new string_list_entry refers to the
93 * input string.
94 */
1d2f80fa 95struct string_list_item *string_list_append(struct string_list *list, const char *string);
e448fed8
MH
96
97/*
98 * Like string_list_append(), except string is never copied. When
99 * list->strdup_strings is set, this function can be used to hand
100 * ownership of a malloc()ed string to list without making an extra
101 * copy.
102 */
103struct string_list_item *string_list_append_nodup(struct string_list *list, char *string);
104
3383e199 105void string_list_sort(struct string_list *list);
c455c87c 106int unsorted_string_list_has_string(struct string_list *list, const char *string);
e2421480
SB
107struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
108 const char *string);
e448fed8 109
86d4b528 110void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
ff919f96
MH
111
112/*
113 * Split string into substrings on character delim and append the
114 * substrings to list. The input string is not modified.
115 * list->strdup_strings must be set, as new memory needs to be
116 * allocated to hold the substrings. If maxsplit is non-negative,
117 * then split at most maxsplit times. Return the number of substrings
118 * appended to list.
119 *
120 * Examples:
121 * string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"]
122 * string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"]
123 * string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"]
124 * string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""]
125 * string_list_split(l, "", ':', -1) -> [""]
126 * string_list_split(l, ":", ':', -1) -> ["", ""]
127 */
128int string_list_split(struct string_list *list, const char *string,
129 int delim, int maxsplit);
130
131/*
132 * Like string_list_split(), except that string is split in-place: the
133 * delimiter characters in string are overwritten with NULs, and the
134 * new string_list_items point into string (which therefore must not
135 * be modified or freed while the string_list is in use).
136 * list->strdup_strings must *not* be set.
137 */
138int string_list_split_in_place(struct string_list *list, char *string,
139 int delim, int maxsplit);
0afcb5f7 140#endif /* STRING_LIST_H */