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