diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2026-06-29 17:43:24 +0000 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2026-06-29 17:43:24 +0000 |
| commit | 15d4dfc538dce7bfce4b0926b77f7f200cca99ff (patch) | |
| tree | f0dbceb404d2bab3896ffd6b42ec4b41f8f01c7e /xs.h | |
| parent | 30849656518854f1ff5af2ad8e583ceb199d1fbd (diff) | |
| parent | 25ccc2599aa722b0552193961d4cda15928112d8 (diff) | |
Diffstat (limited to 'xs.h')
| -rw-r--r-- | xs.h | 61 |
1 files changed, 58 insertions, 3 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); @@ -141,6 +143,7 @@ const char *xs_number_str(const xs_number *v); xs_data *xs_data_new(const void *data, int size); int xs_data_size(const xs_data *value); +const void *xs_data_ptr(const xs_data *value); void xs_data_get(void *data, const xs_data *value); void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); @@ -850,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 */ { @@ -965,10 +1014,9 @@ int xs_list_in(const xs_list *list, const xs_val *val) int n = 0; const xs_val *v; - int sz = xs_size(val); xs_list_foreach(list, v) { - if (sz == xs_size(v) && memcmp(val, v, sz) == 0) + if (xs_cmp(val, v) == 0) return n; n++; @@ -1524,10 +1572,17 @@ int xs_data_size(const xs_data *value) } +const void *xs_data_ptr(const xs_data *value) +/* return an internal pointer to the stored data */ +{ + return &value[1 + _XS_TYPE_SIZE]; +} + + void xs_data_get(void *data, const xs_data *value) /* copies the raw data stored inside value into data */ { - memcpy(data, &value[1 + _XS_TYPE_SIZE], xs_data_size(value)); + memcpy(data, xs_data_ptr(value), xs_data_size(value)); } |