Skip to content

Commit eb5f0c7

Browse files
mhaggergitster
authored andcommitted
string_list: add a new function, filter_string_list()
This function allows entries that don't match a specified criterion to be discarded from a string_list while preserving the order of the remaining entries. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ff919f9 commit eb5f0c7

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

Documentation/technical/api-string-list.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ member (you need this if you add things later) and you should set the
3333
. Can remove individual items of an unsorted list using
3434
`unsorted_string_list_delete_item`.
3535

36+
. Can remove items not matching a criterion from a sorted or unsorted
37+
list using `filter_string_list`.
38+
3639
. Finally it should free the list using `string_list_clear`.
3740

3841
Example:
@@ -61,6 +64,14 @@ Functions
6164

6265
* General ones (works with sorted and unsorted lists as well)
6366

67+
`filter_string_list`::
68+
69+
Apply a function to each item in a list, retaining only the
70+
items for which the function returns true. If free_util is
71+
true, call free() on the util members of any items that have
72+
to be deleted. Preserve the order of the items that are
73+
retained.
74+
6475
`print_string_list`::
6576

6677
Dump a string_list to stdout, useful mainly for debugging purposes. It

string-list.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ int for_each_string_list(struct string_list *list,
102102
return ret;
103103
}
104104

105+
void filter_string_list(struct string_list *list, int free_util,
106+
string_list_each_func_t want, void *cb_data)
107+
{
108+
int src, dst = 0;
109+
for (src = 0; src < list->nr; src++) {
110+
if (want(&list->items[src], cb_data)) {
111+
list->items[dst++] = list->items[src];
112+
} else {
113+
if (list->strdup_strings)
114+
free(list->items[src].string);
115+
if (free_util)
116+
free(list->items[src].util);
117+
}
118+
}
119+
list->nr = dst;
120+
}
121+
105122
void string_list_clear(struct string_list *list, int free_util)
106123
{
107124
if (list->items) {

string-list.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ int for_each_string_list(struct string_list *list,
2929
#define for_each_string_list_item(item,list) \
3030
for (item = (list)->items; item < (list)->items + (list)->nr; ++item)
3131

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+
*/
38+
void filter_string_list(struct string_list *list, int free_util,
39+
string_list_each_func_t want, void *cb_data);
40+
3241

3342
/* Use these functions only on sorted lists: */
3443
int string_list_has_string(const struct string_list *list, const char *string);

t/t0063-string-list.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,15 @@ test_split ":" ":" "-1" <<EOF
6060
[1]: ""
6161
EOF
6262

63+
test_expect_success "test filter_string_list" '
64+
test "x-" = "x$(test-string-list filter - y)" &&
65+
test "x-" = "x$(test-string-list filter no y)" &&
66+
test yes = "$(test-string-list filter yes y)" &&
67+
test yes = "$(test-string-list filter no:yes y)" &&
68+
test yes = "$(test-string-list filter yes:no y)" &&
69+
test y1:y2 = "$(test-string-list filter y1:y2 y)" &&
70+
test y2:y1 = "$(test-string-list filter y2:y1 y)" &&
71+
test "x-" = "x$(test-string-list filter x1:x2 y)"
72+
'
73+
6374
test_done

test-string-list.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
11
#include "cache.h"
22
#include "string-list.h"
33

4+
/*
5+
* Parse an argument into a string list. arg should either be a
6+
* ':'-separated list of strings, or "-" to indicate an empty string
7+
* list (as opposed to "", which indicates a string list containing a
8+
* single empty string). list->strdup_strings must be set.
9+
*/
10+
void parse_string_list(struct string_list *list, const char *arg)
11+
{
12+
if (!strcmp(arg, "-"))
13+
return;
14+
15+
(void)string_list_split(list, arg, ':', -1);
16+
}
17+
418
void write_list(const struct string_list *list)
519
{
620
int i;
721
for (i = 0; i < list->nr; i++)
822
printf("[%d]: \"%s\"\n", i, list->items[i].string);
923
}
1024

25+
void write_list_compact(const struct string_list *list)
26+
{
27+
int i;
28+
if (!list->nr)
29+
printf("-\n");
30+
else {
31+
printf("%s", list->items[0].string);
32+
for (i = 1; i < list->nr; i++)
33+
printf(":%s", list->items[i].string);
34+
printf("\n");
35+
}
36+
}
37+
38+
int prefix_cb(struct string_list_item *item, void *cb_data)
39+
{
40+
const char *prefix = (const char *)cb_data;
41+
return !prefixcmp(item->string, prefix);
42+
}
43+
1144
int main(int argc, char **argv)
1245
{
1346
if (argc == 5 && !strcmp(argv[1], "split")) {
@@ -39,6 +72,21 @@ int main(int argc, char **argv)
3972
return 0;
4073
}
4174

75+
if (argc == 4 && !strcmp(argv[1], "filter")) {
76+
/*
77+
* Retain only the items that have the specified prefix.
78+
* Arguments: list|- prefix
79+
*/
80+
struct string_list list = STRING_LIST_INIT_DUP;
81+
const char *prefix = argv[3];
82+
83+
parse_string_list(&list, argv[2]);
84+
filter_string_list(&list, 0, prefix_cb, (void *)prefix);
85+
write_list_compact(&list);
86+
string_list_clear(&list, 0);
87+
return 0;
88+
}
89+
4290
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
4391
argv[1] ? argv[1] : "(there was none)");
4492
return 1;

0 commit comments

Comments
 (0)