string-list.hon commit push: clarify rejection of update to non-commit-ish (80054cf)
   1#ifndef STRING_LIST_H
   2#define STRING_LIST_H
   3
   4struct string_list_item {
   5        char *string;
   6        void *util;
   7};
   8struct string_list {
   9        struct string_list_item *items;
  10        unsigned int nr, alloc;
  11        unsigned int strdup_strings:1;
  12};
  13
  14#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0 }
  15#define STRING_LIST_INIT_DUP   { NULL, 0, 0, 1 }
  16
  17void print_string_list(const struct string_list *p, const char *text);
  18void string_list_clear(struct string_list *list, int free_util);
  19
  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
  25/* Use this function or the macro below to iterate over each item */
  26typedef int (*string_list_each_func_t)(struct string_list_item *, void *);
  27int 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)
  31
  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
  41/*
  42 * Return the longest string in prefixes that is a prefix (in the
  43 * sense of prefixcmp()) of string, or NULL if no such prefix exists.
  44 * This function does not require the string_list to be sorted (it
  45 * does a linear search).
  46 */
  47char *string_list_longest_prefix(const struct string_list *prefixes, const char *string);
  48
  49
  50/* Use these functions only on sorted lists: */
  51int string_list_has_string(const struct string_list *list, const char *string);
  52int string_list_find_insert_index(const struct string_list *list, const char *string,
  53                                  int negative_existing_index);
  54struct string_list_item *string_list_insert(struct string_list *list, const char *string);
  55struct string_list_item *string_list_insert_at_index(struct string_list *list,
  56                                                     int insert_at, const char *string);
  57struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
  58
  59/*
  60 * Remove all but the first of consecutive entries with the same
  61 * string value.  If free_util is true, call free() on the util
  62 * members of any items that have to be deleted.
  63 */
  64void string_list_remove_duplicates(struct string_list *sorted_list, int free_util);
  65
  66
  67/* Use these functions only on unsorted lists: */
  68
  69/*
  70 * Add string to the end of list.  If list->strdup_string is set, then
  71 * string is copied; otherwise the new string_list_entry refers to the
  72 * input string.
  73 */
  74struct string_list_item *string_list_append(struct string_list *list, const char *string);
  75
  76/*
  77 * Like string_list_append(), except string is never copied.  When
  78 * list->strdup_strings is set, this function can be used to hand
  79 * ownership of a malloc()ed string to list without making an extra
  80 * copy.
  81 */
  82struct string_list_item *string_list_append_nodup(struct string_list *list, char *string);
  83
  84void sort_string_list(struct string_list *list);
  85int unsorted_string_list_has_string(struct string_list *list, const char *string);
  86struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
  87                                                     const char *string);
  88
  89void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
  90
  91/*
  92 * Split string into substrings on character delim and append the
  93 * substrings to list.  The input string is not modified.
  94 * list->strdup_strings must be set, as new memory needs to be
  95 * allocated to hold the substrings.  If maxsplit is non-negative,
  96 * then split at most maxsplit times.  Return the number of substrings
  97 * appended to list.
  98 *
  99 * Examples:
 100 *   string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"]
 101 *   string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"]
 102 *   string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"]
 103 *   string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""]
 104 *   string_list_split(l, "", ':', -1) -> [""]
 105 *   string_list_split(l, ":", ':', -1) -> ["", ""]
 106 */
 107int string_list_split(struct string_list *list, const char *string,
 108                      int delim, int maxsplit);
 109
 110/*
 111 * Like string_list_split(), except that string is split in-place: the
 112 * delimiter characters in string are overwritten with NULs, and the
 113 * new string_list_items point into string (which therefore must not
 114 * be modified or freed while the string_list is in use).
 115 * list->strdup_strings must *not* be set.
 116 */
 117int string_list_split_in_place(struct string_list *list, char *string,
 118                               int delim, int maxsplit);
 119#endif /* STRING_LIST_H */