Skip to content

Commit 31d5451

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eb5f0c7 commit 31d5451

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

Documentation/technical/api-string-list.txt

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ member (you need this if you add things later) and you should set the
3030

3131
. Can sort an unsorted list using `sort_string_list`.
3232

33+
. Can remove duplicate items from a sorted list using
34+
`string_list_remove_duplicates`.
35+
3336
. Can remove individual items of an unsorted list using
3437
`unsorted_string_list_delete_item`.
3538

@@ -108,6 +111,12 @@ write `string_list_insert(...)->util = ...;`.
108111
Look up a given string in the string_list, returning the containing
109112
string_list_item. If the string is not found, NULL is returned.
110113

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+
111120
* Functions for unsorted lists only
112121

113122
`string_list_append`::

string-list.c

+17
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ struct string_list_item *string_list_lookup(struct string_list *list, const char
9292
return list->items + i;
9393
}
9494

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+
95112
int for_each_string_list(struct string_list *list,
96113
string_list_each_func_t fn, void *cb_data)
97114
{

string-list.h

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ struct string_list_item *string_list_insert_at_index(struct string_list *list,
4848
int insert_at, const char *string);
4949
struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
5050

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+
5158

5259
/* Use these functions only on unsorted lists: */
5360

t/t0063-string-list.sh

+17
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,21 @@ test_expect_success "test filter_string_list" '
7171
test "x-" = "x$(test-string-list filter x1:x2 y)"
7272
'
7373

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+
7491
test_done

test-string-list.c

+10
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ int main(int argc, char **argv)
8787
return 0;
8888
}
8989

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+
90100
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
91101
argv[1] ? argv[1] : "(there was none)");
92102
return 1;

0 commit comments

Comments
 (0)