summaryrefslogtreecommitdiff
path: root/xs.h
diff options
context:
space:
mode:
authordefault <nobody@localhost>2024-08-30 19:10:26 +0200
committerdefault <nobody@localhost>2024-08-30 19:10:26 +0200
commit0218e964b0d73340c8d0c5d9e37991359d7c4be9 (patch)
tree9cfc1650abf8780be52dc219e37a311244a294e2 /xs.h
parentbff33fc2eda701ff5d6cd8433c15dfd9aca768f7 (diff)
Backport from xs.
Diffstat (limited to 'xs.h')
-rw-r--r--xs.h21
1 files changed, 11 insertions, 10 deletions
diff --git a/xs.h b/xs.h
index ae851eb..108b276 100644
--- a/xs.h
+++ b/xs.h
@@ -158,6 +158,10 @@ unsigned int xs_hash_func(const char *data, int size);
#define xs_is_true(v) (xs_type((v)) == XSTYPE_TRUE)
#define xs_is_false(v) (xs_type((v)) == XSTYPE_FALSE)
+#define xs_list_foreach(l, v) for (int ct_##__LINE__ = 0; xs_list_next(l, &v, &ct_##__LINE__); )
+#define xs_dict_foreach(l, k, v) for (int ct_##__LINE__ = 0; xs_dict_next(l, &k, &v, &ct_##__LINE__); )
+
+
#ifdef XS_IMPLEMENTATION
void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func)
@@ -813,10 +817,10 @@ int xs_list_len(const xs_list *list)
{
XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
- int c = 0, ct = 0;
+ int c = 0;
const xs_val *v;
- while (xs_list_next(list, &v, &ct))
+ xs_list_foreach(list, v)
c++;
return c;
@@ -831,10 +835,10 @@ const xs_val *xs_list_get(const xs_list *list, int num)
if (num < 0)
num = xs_list_len(list) + num;
- int c = 0, ct = 0;
+ int c = 0;
const xs_val *v;
- while (xs_list_next(list, &v, &ct)) {
+ xs_list_foreach(list, v) {
if (c == num)
return v;
@@ -922,11 +926,10 @@ int xs_list_in(const xs_list *list, const xs_val *val)
XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
int n = 0;
- int ct = 0;
const xs_val *v;
int sz = xs_size(val);
- while (xs_list_next(list, &v, &ct)) {
+ xs_list_foreach(list, v) {
if (sz == xs_size(v) && memcmp(val, v, sz) == 0)
return n;
@@ -945,11 +948,10 @@ xs_str *xs_join(const xs_list *list, const char *sep)
xs_str *s = NULL;
const xs_val *v;
int c = 0;
- int ct = 0;
int offset = 0;
int ssz = strlen(sep);
- while (xs_list_next(list, &v, &ct)) {
+ xs_list_foreach(list, v) {
/* refuse to join non-string values */
if (xs_type(v) == XSTYPE_STRING) {
int sz;
@@ -1277,9 +1279,8 @@ xs_dict *xs_dict_gc(const xs_dict *dict)
xs_dict *nd = xs_dict_new();
const xs_str *k;
const xs_val *v;
- int c = 0;
- while (xs_dict_next(dict, &k, &v, &c)) {
+ xs_dict_foreach(dict, k, v) {
if (xs_type(v) == XSTYPE_DICT) {
xs *sd = xs_dict_gc(v);
nd = xs_dict_set(nd, k, sd);