aboutsummaryrefslogtreecommitdiff
path: root/xs.h
diff options
context:
space:
mode:
authorgrunfink <grunfink@comam.es>2026-06-24 11:39:39 +0200
committergrunfink <grunfink@comam.es>2026-06-24 11:39:39 +0200
commit0b3e9ebd7342170fefd27a2ab4b7e243d8a750a5 (patch)
tree3137661677480dea00767fdf0396e31e89e422e3 /xs.h
parent84084ee0246e3a7f64eca977a77f2b4ae32bb6d3 (diff)
Moved xs_list_reverse() to xs.h.
Diffstat (limited to 'xs.h')
-rw-r--r--xs.h48
1 files changed, 48 insertions, 0 deletions
diff --git a/xs.h b/xs.h
index d286a22..8d8dd9c 100644
--- a/xs.h
+++ b/xs.h
@@ -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 */
{