mirror of
https://github.com/git/git.git
synced 2024-11-24 02:17:02 +08:00
c455c87c5c
The name path_list was correct for the first usage of that data structure, but it really is a general-purpose string list. $ perl -i -pe 's/path-list/string-list/g' $(git grep -l path-list) $ perl -i -pe 's/path_list/string_list/g' $(git grep -l path_list) $ git mv path-list.h string-list.h $ git mv path-list.c string-list.c $ perl -i -pe 's/has_path/has_string/g' $(git grep -l has_path) $ perl -i -pe 's/path/string/g' string-list.[ch] $ git mv Documentation/technical/api-path-list.txt \ Documentation/technical/api-string-list.txt $ perl -i -pe 's/strdup_paths/strdup_strings/g' $(git grep -l strdup_paths) ... and then fix all users of string-list to access the member "string" instead of "path". Documentation/technical/api-string-list.txt needed some rewrapping, too. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
135 lines
3.1 KiB
C
135 lines
3.1 KiB
C
#include "cache.h"
|
|
#include "string-list.h"
|
|
|
|
/* if there is no exact match, point to the index where the entry could be
|
|
* inserted */
|
|
static int get_entry_index(const struct string_list *list, const char *string,
|
|
int *exact_match)
|
|
{
|
|
int left = -1, right = list->nr;
|
|
|
|
while (left + 1 < right) {
|
|
int middle = (left + right) / 2;
|
|
int compare = strcmp(string, list->items[middle].string);
|
|
if (compare < 0)
|
|
right = middle;
|
|
else if (compare > 0)
|
|
left = middle;
|
|
else {
|
|
*exact_match = 1;
|
|
return middle;
|
|
}
|
|
}
|
|
|
|
*exact_match = 0;
|
|
return right;
|
|
}
|
|
|
|
/* returns -1-index if already exists */
|
|
static int add_entry(struct string_list *list, const char *string)
|
|
{
|
|
int exact_match;
|
|
int index = get_entry_index(list, string, &exact_match);
|
|
|
|
if (exact_match)
|
|
return -1 - index;
|
|
|
|
if (list->nr + 1 >= list->alloc) {
|
|
list->alloc += 32;
|
|
list->items = xrealloc(list->items, list->alloc
|
|
* sizeof(struct string_list_item));
|
|
}
|
|
if (index < list->nr)
|
|
memmove(list->items + index + 1, list->items + index,
|
|
(list->nr - index)
|
|
* sizeof(struct string_list_item));
|
|
list->items[index].string = list->strdup_strings ?
|
|
xstrdup(string) : (char *)string;
|
|
list->items[index].util = NULL;
|
|
list->nr++;
|
|
|
|
return index;
|
|
}
|
|
|
|
struct string_list_item *string_list_insert(const char *string, struct string_list *list)
|
|
{
|
|
int index = add_entry(list, string);
|
|
|
|
if (index < 0)
|
|
index = -1 - index;
|
|
|
|
return list->items + index;
|
|
}
|
|
|
|
int string_list_has_string(const struct string_list *list, const char *string)
|
|
{
|
|
int exact_match;
|
|
get_entry_index(list, string, &exact_match);
|
|
return exact_match;
|
|
}
|
|
|
|
struct string_list_item *string_list_lookup(const char *string, struct string_list *list)
|
|
{
|
|
int exact_match, i = get_entry_index(list, string, &exact_match);
|
|
if (!exact_match)
|
|
return NULL;
|
|
return list->items + i;
|
|
}
|
|
|
|
void string_list_clear(struct string_list *list, int free_util)
|
|
{
|
|
if (list->items) {
|
|
int i;
|
|
if (list->strdup_strings) {
|
|
for (i = 0; i < list->nr; i++)
|
|
free(list->items[i].string);
|
|
}
|
|
if (free_util) {
|
|
for (i = 0; i < list->nr; i++)
|
|
free(list->items[i].util);
|
|
}
|
|
free(list->items);
|
|
}
|
|
list->items = NULL;
|
|
list->nr = list->alloc = 0;
|
|
}
|
|
|
|
void print_string_list(const char *text, const struct string_list *p)
|
|
{
|
|
int i;
|
|
if ( text )
|
|
printf("%s\n", text);
|
|
for (i = 0; i < p->nr; i++)
|
|
printf("%s:%p\n", p->items[i].string, p->items[i].util);
|
|
}
|
|
|
|
struct string_list_item *string_list_append(const char *string, struct string_list *list)
|
|
{
|
|
ALLOC_GROW(list->items, list->nr + 1, list->alloc);
|
|
list->items[list->nr].string =
|
|
list->strdup_strings ? xstrdup(string) : (char *)string;
|
|
return list->items + list->nr++;
|
|
}
|
|
|
|
static int cmp_items(const void *a, const void *b)
|
|
{
|
|
const struct string_list_item *one = a;
|
|
const struct string_list_item *two = b;
|
|
return strcmp(one->string, two->string);
|
|
}
|
|
|
|
void sort_string_list(struct string_list *list)
|
|
{
|
|
qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
|
|
}
|
|
|
|
int unsorted_string_list_has_string(struct string_list *list, const char *string)
|
|
{
|
|
int i;
|
|
for (i = 0; i < list->nr; i++)
|
|
if (!strcmp(string, list->items[i].string))
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|