string-list.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #ifndef STRING_LIST_H
  2. #define STRING_LIST_H
  3. /**
  4. * The string_list API offers a data structure and functions to handle
  5. * sorted and unsorted arrays of strings. A "sorted" list is one whose
  6. * entries are sorted by string value in the order specified by the `cmp`
  7. * member (`strcmp()` by default).
  8. *
  9. * The caller:
  10. *
  11. * . Allocates and clears a `struct string_list` variable.
  12. *
  13. * . Initializes the members. You might want to set the flag `strdup_strings`
  14. * if the strings should be strdup()ed. For example, this is necessary
  15. * when you add something like git_path("..."), since that function returns
  16. * a static buffer that will change with the next call to git_path().
  17. *
  18. * If you need something advanced, you can manually malloc() the `items`
  19. * member (you need this if you add things later) and you should set the
  20. * `nr` and `alloc` members in that case, too.
  21. *
  22. * . Adds new items to the list, using `string_list_append`,
  23. * `string_list_append_nodup`, `string_list_insert`,
  24. * `string_list_split`, and/or `string_list_split_in_place`.
  25. *
  26. * . Can check if a string is in the list using `string_list_has_string` or
  27. * `unsorted_string_list_has_string` and get it from the list using
  28. * `string_list_lookup` for sorted lists.
  29. *
  30. * . Can sort an unsorted list using `string_list_sort`.
  31. *
  32. * . Can remove duplicate items from a sorted list using
  33. * `string_list_remove_duplicates`.
  34. *
  35. * . Can remove individual items of an unsorted list using
  36. * `unsorted_string_list_delete_item`.
  37. *
  38. * . Can remove items not matching a criterion from a sorted or unsorted
  39. * list using `filter_string_list`, or remove empty strings using
  40. * `string_list_remove_empty_items`.
  41. *
  42. * . Finally it should free the list using `string_list_clear`.
  43. *
  44. * Example:
  45. *
  46. * struct string_list list = STRING_LIST_INIT_NODUP;
  47. * int i;
  48. *
  49. * string_list_append(&list, "foo");
  50. * string_list_append(&list, "bar");
  51. * for (i = 0; i < list.nr; i++)
  52. * printf("%s\n", list.items[i].string)
  53. *
  54. * NOTE: It is more efficient to build an unsorted list and sort it
  55. * afterwards, instead of building a sorted list (`O(n log n)` instead of
  56. * `O(n^2)`).
  57. *
  58. * However, if you use the list to check if a certain string was added
  59. * already, you should not do that (using unsorted_string_list_has_string()),
  60. * because the complexity would be quadratic again (but with a worse factor).
  61. */
  62. /**
  63. * Represents an item of the list. The `string` member is a pointer to the
  64. * string, and you may use the `util` member for any purpose, if you want.
  65. */
  66. struct string_list_item {
  67. char *string;
  68. void *util;
  69. };
  70. typedef int (*compare_strings_fn)(const char *, const char *);
  71. /**
  72. * Represents the list itself.
  73. *
  74. * . The array of items are available via the `items` member.
  75. * . The `nr` member contains the number of items stored in the list.
  76. * . The `alloc` member is used to avoid reallocating at every insertion.
  77. * You should not tamper with it.
  78. * . Setting the `strdup_strings` member to 1 will strdup() the strings
  79. * before adding them, see above.
  80. * . The `compare_strings_fn` member is used to specify a custom compare
  81. * function, otherwise `strcmp()` is used as the default function.
  82. */
  83. struct string_list {
  84. struct string_list_item *items;
  85. unsigned int nr, alloc;
  86. unsigned int strdup_strings:1;
  87. compare_strings_fn cmp; /* NULL uses strcmp() */
  88. };
  89. #define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0, NULL }
  90. #define STRING_LIST_INIT_DUP { NULL, 0, 0, 1, NULL }
  91. /* General functions which work with both sorted and unsorted lists. */
  92. /**
  93. * Initialize the members of the string_list, set `strdup_strings`
  94. * member according to the value of the second parameter.
  95. */
  96. void string_list_init(struct string_list *list, int strdup_strings);
  97. /** Callback function type for for_each_string_list */
  98. typedef int (*string_list_each_func_t)(struct string_list_item *, void *);
  99. /**
  100. * Apply `want` to each item in `list`, retaining only the ones for which
  101. * the function returns true. If `free_util` is true, call free() on
  102. * the util members of any items that have to be deleted. Preserve
  103. * the order of the items that are retained.
  104. */
  105. void filter_string_list(struct string_list *list, int free_util,
  106. string_list_each_func_t want, void *cb_data);
  107. /**
  108. * Free a string_list. The `string` pointer of the items will be freed
  109. * in case the `strdup_strings` member of the string_list is set. The
  110. * second parameter controls if the `util` pointer of the items should
  111. * be freed or not.
  112. */
  113. void string_list_clear(struct string_list *list, int free_util);
  114. /**
  115. * Callback type for `string_list_clear_func`. The string associated
  116. * with the util pointer is passed as the second argument
  117. */
  118. typedef void (*string_list_clear_func_t)(void *p, const char *str);
  119. /** Call a custom clear function on each util pointer */
  120. void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);
  121. /**
  122. * Apply `func` to each item. If `func` returns nonzero, the
  123. * iteration aborts and the return value is propagated.
  124. */
  125. int for_each_string_list(struct string_list *list,
  126. string_list_each_func_t func, void *cb_data);
  127. /** Iterate over each item, as a macro. */
  128. #define for_each_string_list_item(item,list) \
  129. for (item = (list)->items; \
  130. item && item < (list)->items + (list)->nr; \
  131. ++item)
  132. /**
  133. * Remove any empty strings from the list. If free_util is true, call
  134. * free() on the util members of any items that have to be deleted.
  135. * Preserve the order of the items that are retained.
  136. */
  137. void string_list_remove_empty_items(struct string_list *list, int free_util);
  138. /* Use these functions only on sorted lists: */
  139. /** Determine if the string_list has a given string or not. */
  140. int string_list_has_string(const struct string_list *list, const char *string);
  141. int string_list_find_insert_index(const struct string_list *list, const char *string,
  142. int negative_existing_index);
  143. /**
  144. * Insert a new element to the string_list. The returned pointer can
  145. * be handy if you want to write something to the `util` pointer of
  146. * the string_list_item containing the just added string. If the given
  147. * string already exists the insertion will be skipped and the pointer
  148. * to the existing item returned.
  149. *
  150. * Since this function uses xrealloc() (which die()s if it fails) if the
  151. * list needs to grow, it is safe not to check the pointer. I.e. you may
  152. * write `string_list_insert(...)->util = ...;`.
  153. */
  154. struct string_list_item *string_list_insert(struct string_list *list, const char *string);
  155. /**
  156. * Remove the given string from the sorted list. If the string
  157. * doesn't exist, the list is not altered.
  158. */
  159. void string_list_remove(struct string_list *list, const char *string,
  160. int free_util);
  161. /**
  162. * Check if the given string is part of a sorted list. If it is part of the list,
  163. * return the corresponding string_list_item, NULL otherwise.
  164. */
  165. struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
  166. /*
  167. * Remove all but the first of consecutive entries with the same
  168. * string value. If free_util is true, call free() on the util
  169. * members of any items that have to be deleted.
  170. */
  171. void string_list_remove_duplicates(struct string_list *sorted_list, int free_util);
  172. /* Use these functions only on unsorted lists: */
  173. /**
  174. * Add string to the end of list. If list->strdup_string is set, then
  175. * string is copied; otherwise the new string_list_entry refers to the
  176. * input string.
  177. */
  178. struct string_list_item *string_list_append(struct string_list *list, const char *string);
  179. /**
  180. * Like string_list_append(), except string is never copied. When
  181. * list->strdup_strings is set, this function can be used to hand
  182. * ownership of a malloc()ed string to list without making an extra
  183. * copy.
  184. */
  185. struct string_list_item *string_list_append_nodup(struct string_list *list, char *string);
  186. /**
  187. * Sort the list's entries by string value in order specified by list->cmp
  188. * (strcmp() if list->cmp is NULL).
  189. */
  190. void string_list_sort(struct string_list *list);
  191. /**
  192. * Like `string_list_has_string()` but for unsorted lists. Linear in
  193. * size of the list.
  194. */
  195. int unsorted_string_list_has_string(struct string_list *list, const char *string);
  196. /**
  197. * Like `string_list_lookup()` but for unsorted lists. Linear in size
  198. * of the list.
  199. */
  200. struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
  201. const char *string);
  202. /**
  203. * Remove an item from a string_list. The `string` pointer of the
  204. * items will be freed in case the `strdup_strings` member of the
  205. * string_list is set. The third parameter controls if the `util`
  206. * pointer of the items should be freed or not.
  207. */
  208. void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
  209. /**
  210. * Split string into substrings on character `delim` and append the
  211. * substrings to `list`. The input string is not modified.
  212. * list->strdup_strings must be set, as new memory needs to be
  213. * allocated to hold the substrings. If maxsplit is non-negative,
  214. * then split at most maxsplit times. Return the number of substrings
  215. * appended to list.
  216. *
  217. * Examples:
  218. * string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"]
  219. * string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"]
  220. * string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"]
  221. * string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""]
  222. * string_list_split(l, "", ':', -1) -> [""]
  223. * string_list_split(l, ":", ':', -1) -> ["", ""]
  224. */
  225. int string_list_split(struct string_list *list, const char *string,
  226. int delim, int maxsplit);
  227. /*
  228. * Like string_list_split(), except that string is split in-place: the
  229. * delimiter characters in string are overwritten with NULs, and the
  230. * new string_list_items point into string (which therefore must not
  231. * be modified or freed while the string_list is in use).
  232. * list->strdup_strings must *not* be set.
  233. */
  234. int string_list_split_in_place(struct string_list *list, char *string,
  235. int delim, int maxsplit);
  236. #endif /* STRING_LIST_H */