Backport from xs.

This commit is contained in:
default 2023-01-24 14:57:04 +01:00
parent af604f91a9
commit 4baf56c4ce
2 changed files with 14 additions and 6 deletions

18
xs.h
View File

@ -76,7 +76,9 @@ d_char *xs_list_del(d_char *list, int num);
d_char *xs_list_insert(d_char *list, int num, const char *data); d_char *xs_list_insert(d_char *list, int num, const char *data);
d_char *xs_list_insert_sorted(d_char *list, const char *str); d_char *xs_list_insert_sorted(d_char *list, const char *str);
d_char *xs_list_set(d_char *list, int num, const char *data); d_char *xs_list_set(d_char *list, int num, const char *data);
d_char *xs_list_pop(d_char *list, char **data); d_char *xs_list_dequeue(d_char *list, char **data, int last);
#define xs_list_pop(list, data) xs_list_dequeue(list, data, 1)
#define xs_list_shift(list, data) xs_list_dequeue(list, data, 0)
int xs_list_in(char *list, const char *val); int xs_list_in(char *list, const char *val);
d_char *xs_join(char *list, const char *sep); d_char *xs_join(char *list, const char *sep);
d_char *xs_split_n(const char *str, const char *sep, int times); d_char *xs_split_n(const char *str, const char *sep, int times);
@ -665,13 +667,19 @@ d_char *xs_list_set(d_char *list, int num, const char *data)
} }
d_char *xs_list_pop(d_char *list, char **data) d_char *xs_list_dequeue(d_char *list, char **data, int last)
/* pops the last element from the list */ /* gets a copy of the first or last element of a list, shrinking it */
{ {
char *p = list, *v = NULL; char *p = list, *v = NULL;
/* iterate to the end */ if (!last) {
while (xs_list_iter(&p, &v)); /* get the first */
xs_list_iter(&p, &v);
}
else {
/* iterate to the end */
while (xs_list_iter(&p, &v));
}
if (v != NULL) { if (v != NULL) {
*data = xs_dup(v); *data = xs_dup(v);

View File

@ -1 +1 @@
/* 366ce5817597e9f4d735bced6b8c40ecc897608e */ /* e8b70248081aa9086f64861bc85a67f390c41afb */