diff options
| author | grunfink <grunfink@comam.es> | 2026-06-24 11:39:39 +0200 |
|---|---|---|
| committer | grunfink <grunfink@comam.es> | 2026-06-24 11:39:39 +0200 |
| commit | 0b3e9ebd7342170fefd27a2ab4b7e243d8a750a5 (patch) | |
| tree | 3137661677480dea00767fdf0396e31e89e422e3 | |
| parent | 84084ee0246e3a7f64eca977a77f2b4ae32bb6d3 (diff) | |
Moved xs_list_reverse() to xs.h.
| -rw-r--r-- | xs.h | 48 | ||||
| -rw-r--r-- | xs_list_tools.h | 24 | ||||
| -rw-r--r-- | xs_version.h | 2 |
3 files changed, 49 insertions, 25 deletions
@@ -98,6 +98,8 @@ xs_list *_xs_list_append(xs_list *list, const xs_val *vals[]); #define xs_list_append(list, ...) _xs_list_append(list, (const xs_val *[]){ __VA_ARGS__, NULL }) int xs_list_iter(xs_list **list, const xs_val **value); int xs_list_next(const xs_list *list, const xs_val **value, int *ctxt); +int xs_list_cap(xs_list *list, int max); +xs_list *xs_list_reverse(const xs_list *l); int xs_list_len(const xs_list *list); const xs_val *xs_list_get(const xs_list *list, int num); xs_list *xs_list_del(xs_list *list, int num); @@ -851,6 +853,52 @@ int xs_list_next(const xs_list *list, const xs_val **value, int *ctxt) } +int xs_list_cap(xs_list *list, int max) +/* caps the list to have a maximum of max items */ +{ + int n, ctxt = 0; + const char *v; + + /* advance upto max elements */ + for (n = 0; n < max + 1 && xs_list_next(list, &v, &ctxt); n++); + + /* more than max items? */ + if (n == max + 1) { + /* insert an end of list over the XSTYPE_LITEM of the old item */ + char *p = (char *)v; + p[-1] = '\0'; + + /* rewrite the list len */ + _xs_put_size(list, p - list); + } + + return n == max + 1; +} + + +xs_list *xs_list_reverse(const xs_list *l) +/* creates a new list as a reverse version of l */ +{ + xs_list *n = xs_dup(l); + const xs_val *v; + + /* move to one byte before the EOM */ + char *p = n + xs_size(n) - 1; + + xs_list_foreach(l, v) { + /* size of v, plus the LITEM */ + int z = xs_size(v) + 1; + + p -= z; + + /* copy v, including its LITEM */ + memcpy(p, v - 1, z); + } + + return n; +} + + int xs_list_len(const xs_list *list) /* returns the number of elements in the list */ { diff --git a/xs_list_tools.h b/xs_list_tools.h index 33d4b87..f47d782 100644 --- a/xs_list_tools.h +++ b/xs_list_tools.h @@ -5,7 +5,6 @@ #define _XS_LIST_TOOLS_H xs_list *xs_list_insert_sorted(xs_list *list, const xs_val *nv); - xs_list *xs_list_reverse(const xs_list *l); xs_val **xs_list_to_array(const xs_list *l, int *len); int xs_list_sort_cmp(const void *p1, const void *p2); int xs_list_sort_inv_cmp(const void *p1, const void *p2); @@ -37,29 +36,6 @@ xs_list *xs_list_insert_sorted(xs_list *list, const xs_val *nv) } -xs_list *xs_list_reverse(const xs_list *l) -/* creates a new list as a reverse version of l */ -{ - xs_list *n = xs_dup(l); - const xs_val *v; - - /* move to one byte before the EOM */ - char *p = n + xs_size(n) - 1; - - xs_list_foreach(l, v) { - /* size of v, plus the LITEM */ - int z = xs_size(v) + 1; - - p -= z; - - /* copy v, including its LITEM */ - memcpy(p, v - 1, z); - } - - return n; -} - - xs_val **xs_list_to_array(const xs_list *l, int *len) /* converts a list to an array of values */ /* must be freed after use */ diff --git a/xs_version.h b/xs_version.h index 9794cb5..500b156 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* 2e209276f3f598dbee88350a33291198ab792eb2 2026-06-03T19:54:14+02:00 */ +/* 4110cac45d3d8a6c86b6552c59ec9c73d7a3a4c0 2026-06-17T11:46:41+02:00 */ |