t / helper / test-string-list.con commit range-diff: allow to diff files regardless of submodule config (0e573e8)
   1#include "test-tool.h"
   2#include "cache.h"
   3#include "string-list.h"
   4
   5/*
   6 * Parse an argument into a string list.  arg should either be a
   7 * ':'-separated list of strings, or "-" to indicate an empty string
   8 * list (as opposed to "", which indicates a string list containing a
   9 * single empty string).  list->strdup_strings must be set.
  10 */
  11static void parse_string_list(struct string_list *list, const char *arg)
  12{
  13        if (!strcmp(arg, "-"))
  14                return;
  15
  16        (void)string_list_split(list, arg, ':', -1);
  17}
  18
  19static void write_list(const struct string_list *list)
  20{
  21        int i;
  22        for (i = 0; i < list->nr; i++)
  23                printf("[%d]: \"%s\"\n", i, list->items[i].string);
  24}
  25
  26static void write_list_compact(const struct string_list *list)
  27{
  28        int i;
  29        if (!list->nr)
  30                printf("-\n");
  31        else {
  32                printf("%s", list->items[0].string);
  33                for (i = 1; i < list->nr; i++)
  34                        printf(":%s", list->items[i].string);
  35                printf("\n");
  36        }
  37}
  38
  39static int prefix_cb(struct string_list_item *item, void *cb_data)
  40{
  41        const char *prefix = (const char *)cb_data;
  42        return starts_with(item->string, prefix);
  43}
  44
  45int cmd__string_list(int argc, const char **argv)
  46{
  47        if (argc == 5 && !strcmp(argv[1], "split")) {
  48                struct string_list list = STRING_LIST_INIT_DUP;
  49                int i;
  50                const char *s = argv[2];
  51                int delim = *argv[3];
  52                int maxsplit = atoi(argv[4]);
  53
  54                i = string_list_split(&list, s, delim, maxsplit);
  55                printf("%d\n", i);
  56                write_list(&list);
  57                string_list_clear(&list, 0);
  58                return 0;
  59        }
  60
  61        if (argc == 5 && !strcmp(argv[1], "split_in_place")) {
  62                struct string_list list = STRING_LIST_INIT_NODUP;
  63                int i;
  64                char *s = xstrdup(argv[2]);
  65                int delim = *argv[3];
  66                int maxsplit = atoi(argv[4]);
  67
  68                i = string_list_split_in_place(&list, s, delim, maxsplit);
  69                printf("%d\n", i);
  70                write_list(&list);
  71                string_list_clear(&list, 0);
  72                free(s);
  73                return 0;
  74        }
  75
  76        if (argc == 4 && !strcmp(argv[1], "filter")) {
  77                /*
  78                 * Retain only the items that have the specified prefix.
  79                 * Arguments: list|- prefix
  80                 */
  81                struct string_list list = STRING_LIST_INIT_DUP;
  82                const char *prefix = argv[3];
  83
  84                parse_string_list(&list, argv[2]);
  85                filter_string_list(&list, 0, prefix_cb, (void *)prefix);
  86                write_list_compact(&list);
  87                string_list_clear(&list, 0);
  88                return 0;
  89        }
  90
  91        if (argc == 3 && !strcmp(argv[1], "remove_duplicates")) {
  92                struct string_list list = STRING_LIST_INIT_DUP;
  93
  94                parse_string_list(&list, argv[2]);
  95                string_list_remove_duplicates(&list, 0);
  96                write_list_compact(&list);
  97                string_list_clear(&list, 0);
  98                return 0;
  99        }
 100
 101        if (argc == 2 && !strcmp(argv[1], "sort")) {
 102                struct string_list list = STRING_LIST_INIT_NODUP;
 103                struct strbuf sb = STRBUF_INIT;
 104                struct string_list_item *item;
 105
 106                strbuf_read(&sb, 0, 0);
 107
 108                /*
 109                 * Split by newline, but don't create a string_list item
 110                 * for the empty string after the last separator.
 111                 */
 112                if (sb.len && sb.buf[sb.len - 1] == '\n')
 113                        strbuf_setlen(&sb, sb.len - 1);
 114                string_list_split_in_place(&list, sb.buf, '\n', -1);
 115
 116                string_list_sort(&list);
 117
 118                for_each_string_list_item(item, &list)
 119                        puts(item->string);
 120
 121                string_list_clear(&list, 0);
 122                strbuf_release(&sb);
 123                return 0;
 124        }
 125
 126        fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
 127                argv[1] ? argv[1] : "(there was none)");
 128        return 1;
 129}