Sync with 1.8.0.2
[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};
9cba13ca 8struct string_list {
c455c87c
JS
9 struct string_list_item *items;
10 unsigned int nr, alloc;
11 unsigned int strdup_strings:1;
12};
13
183113a5
TF
14#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0 }
15#define STRING_LIST_INIT_DUP { NULL, 0, 0, 1 }
16
cb944f6b 17void print_string_list(const struct string_list *p, const char *text);
c455c87c
JS
18void string_list_clear(struct string_list *list, int free_util);
19
cfa1ee6b
MSO
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 */
22typedef void (*string_list_clear_func_t)(void *p, const char *str);
23void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);
24
8d31635c 25/* Use this function or the macro below to iterate over each item */
c6f5a7a9 26typedef int (*string_list_each_func_t)(struct string_list_item *, void *);
b684e977
JP
27int for_each_string_list(struct string_list *list,
28 string_list_each_func_t, void *cb_data);
8d31635c
AR
29#define for_each_string_list_item(item,list) \
30 for (item = (list)->items; item < (list)->items + (list)->nr; ++item)
c6f5a7a9 31
eb5f0c7a
MH
32/*
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.
37 */
38void filter_string_list(struct string_list *list, int free_util,
39 string_list_each_func_t want, void *cb_data);
40
6bb2a137
MH
41/*
42 * Remove any empty strings from the list. If free_util is true, call
43 * free() on the util members of any items that have to be deleted.
44 * Preserve the order of the items that are retained.
45 */
46void string_list_remove_empty_items(struct string_list *list, int free_util);
47
f103f95b
MH
48/*
49 * Return the longest string in prefixes that is a prefix (in the
50 * sense of prefixcmp()) of string, or NULL if no such prefix exists.
51 * This function does not require the string_list to be sorted (it
52 * does a linear search).
53 */
54char *string_list_longest_prefix(const struct string_list *prefixes, const char *string);
55
e448fed8 56
c455c87c
JS
57/* Use these functions only on sorted lists: */
58int string_list_has_string(const struct string_list *list, const char *string);
cfa1ee6b
MSO
59int string_list_find_insert_index(const struct string_list *list, const char *string,
60 int negative_existing_index);
78a395d3 61struct string_list_item *string_list_insert(struct string_list *list, const char *string);
aadceea6
JP
62struct string_list_item *string_list_insert_at_index(struct string_list *list,
63 int insert_at, const char *string);
e8c8b713 64struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
c455c87c 65
31d5451e
MH
66/*
67 * Remove all but the first of consecutive entries with the same
68 * string value. If free_util is true, call free() on the util
69 * members of any items that have to be deleted.
70 */
71void string_list_remove_duplicates(struct string_list *sorted_list, int free_util);
72
e448fed8 73
c455c87c 74/* Use these functions only on unsorted lists: */
e448fed8
MH
75
76/*
77 * Add string to the end of list. If list->strdup_string is set, then
78 * string is copied; otherwise the new string_list_entry refers to the
79 * input string.
80 */
1d2f80fa 81struct string_list_item *string_list_append(struct string_list *list, const char *string);
e448fed8
MH
82
83/*
84 * Like string_list_append(), except string is never copied. When
85 * list->strdup_strings is set, this function can be used to hand
86 * ownership of a malloc()ed string to list without making an extra
87 * copy.
88 */
89struct string_list_item *string_list_append_nodup(struct string_list *list, char *string);
90
c455c87c
JS
91void sort_string_list(struct string_list *list);
92int unsorted_string_list_has_string(struct string_list *list, const char *string);
e2421480
SB
93struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
94 const char *string);
e448fed8 95
86d4b528 96void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
ff919f96
MH
97
98/*
99 * Split string into substrings on character delim and append the
100 * substrings to list. The input string is not modified.
101 * list->strdup_strings must be set, as new memory needs to be
102 * allocated to hold the substrings. If maxsplit is non-negative,
103 * then split at most maxsplit times. Return the number of substrings
104 * appended to list.
105 *
106 * Examples:
107 * string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"]
108 * string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"]
109 * string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"]
110 * string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""]
111 * string_list_split(l, "", ':', -1) -> [""]
112 * string_list_split(l, ":", ':', -1) -> ["", ""]
113 */
114int string_list_split(struct string_list *list, const char *string,
115 int delim, int maxsplit);
116
117/*
118 * Like string_list_split(), except that string is split in-place: the
119 * delimiter characters in string are overwritten with NULs, and the
120 * new string_list_items point into string (which therefore must not
121 * be modified or freed while the string_list is in use).
122 * list->strdup_strings must *not* be set.
123 */
124int string_list_split_in_place(struct string_list *list, char *string,
125 int delim, int maxsplit);
0afcb5f7 126#endif /* STRING_LIST_H */