string_list: add a new function, string_list_remove_duplicates()

Add a function that deletes duplicate entries from a sorted string_list. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 12, 2012 at 16:04 UTC 31d5451eed2677531c80177ff9dc8f5285f5a187
5 files changed +60
Documentation/technical/api-string-list.txt
+9
@@ -30,6 +30,9 @@ member (you need this if you add things later) and you should set the
30
31 . Can sort an unsorted list using `sort_string_list`.
32
33 +. Can remove duplicate items from a sorted list using
34 + `string_list_remove_duplicates`.
35 +
36 . Can remove individual items of an unsorted list using
37 `unsorted_string_list_delete_item`.
38
@@ -108,6 +111,12 @@ write `string_list_insert(...)->util = ...;`.
111 Look up a given string in the string_list, returning the containing
112 string_list_item. If the string is not found, NULL is returned.
113
114 +`string_list_remove_duplicates`::
115 +
116 + Remove all but the first of consecutive entries that have the
117 + same string value. If free_util is true, call free() on the
118 + util members of any items that have to be deleted.
119 +
120 * Functions for unsorted lists only
121
122 `string_list_append`::
string-list.c
+17
@@ -92,6 +92,23 @@ struct string_list_item *string_list_lookup(struct string_list *list, const char
92 return list->items + i;
93 }
94
95 +void string_list_remove_duplicates(struct string_list *list, int free_util)
96 +{
97 + if (list->nr > 1) {
98 + int src, dst;
99 + for (src = dst = 1; src < list->nr; src++) {
100 + if (!strcmp(list->items[dst - 1].string, list->items[src].string)) {
101 + if (list->strdup_strings)
102 + free(list->items[src].string);
103 + if (free_util)
104 + free(list->items[src].util);
105 + } else
106 + list->items[dst++] = list->items[src];
107 + }
108 + list->nr = dst;
109 + }
110 +}
111 +
112 int for_each_string_list(struct string_list *list,
113 string_list_each_func_t fn, void *cb_data)
114 {
string-list.h
+7
@@ -48,6 +48,13 @@ struct string_list_item *string_list_insert_at_index(struct string_list *list,
48 int insert_at, const char *string);
49 struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
50
51 +/*
52 + * Remove all but the first of consecutive entries with the same
53 + * string value. If free_util is true, call free() on the util
54 + * members of any items that have to be deleted.
55 + */
56 +void string_list_remove_duplicates(struct string_list *sorted_list, int free_util);
57 +
58
59 /* Use these functions only on unsorted lists: */
60
t/t0063-string-list.sh
+17
@@ -71,4 +71,21 @@ test_expect_success "test filter_string_list" '
71 test "x-" = "x$(test-string-list filter x1:x2 y)"
72 '
73
74 +test_expect_success "test remove_duplicates" '
75 + test "x-" = "x$(test-string-list remove_duplicates -)" &&
76 + test "x" = "x$(test-string-list remove_duplicates "")" &&
77 + test a = "$(test-string-list remove_duplicates a)" &&
78 + test a = "$(test-string-list remove_duplicates a:a)" &&
79 + test a = "$(test-string-list remove_duplicates a:a:a:a:a)" &&
80 + test a:b = "$(test-string-list remove_duplicates a:b)" &&
81 + test a:b = "$(test-string-list remove_duplicates a:a:b)" &&
82 + test a:b = "$(test-string-list remove_duplicates a:b:b)" &&
83 + test a:b:c = "$(test-string-list remove_duplicates a:b:c)" &&
84 + test a:b:c = "$(test-string-list remove_duplicates a:a:b:c)" &&
85 + test a:b:c = "$(test-string-list remove_duplicates a:b:b:c)" &&
86 + test a:b:c = "$(test-string-list remove_duplicates a:b:c:c)" &&
87 + test a:b:c = "$(test-string-list remove_duplicates a:a:b:b:c:c)" &&
88 + test a:b:c = "$(test-string-list remove_duplicates a:a:a:b:b:b:c:c:c)"
89 +'
90 +
91 test_done
test-string-list.c
+10
@@ -87,6 +87,16 @@ int main(int argc, char **argv)
87 return 0;
88 }
89
90 + if (argc == 3 && !strcmp(argv[1], "remove_duplicates")) {
91 + struct string_list list = STRING_LIST_INIT_DUP;
92 +
93 + parse_string_list(&list, argv[2]);
94 + string_list_remove_duplicates(&list, 0);
95 + write_list_compact(&list);
96 + string_list_clear(&list, 0);
97 + return 0;
98 + }
99 +
100 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
101 argv[1] ? argv[1] : "(there was none)");
102 return 1;