summaryrefslogtreecommitdiff
path: root/xs.h
diff options
context:
space:
mode:
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);
158#define xs_is_true(v) (xs_type((v)) == XSTYPE_TRUE) 158#define xs_is_true(v) (xs_type((v)) == XSTYPE_TRUE)
159#define xs_is_false(v) (xs_type((v)) == XSTYPE_FALSE) 159#define xs_is_false(v) (xs_type((v)) == XSTYPE_FALSE)
160 160
161#define xs_list_foreach(l, v) for (int ct_##__LINE__ = 0; xs_list_next(l, &v, &ct_##__LINE__); )
162#define xs_dict_foreach(l, k, v) for (int ct_##__LINE__ = 0; xs_dict_next(l, &k, &v, &ct_##__LINE__); )
163
164
161#ifdef XS_IMPLEMENTATION 165#ifdef XS_IMPLEMENTATION
162 166
163void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func) 167void *_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)
813{ 817{
814 XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST); 818 XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
815 819
816 int c = 0, ct = 0; 820 int c = 0;
817 const xs_val *v; 821 const xs_val *v;
818 822
819 while (xs_list_next(list, &v, &ct)) 823 xs_list_foreach(list, v)
820 c++; 824 c++;
821 825
822 return c; 826 return c;
@@ -831,10 +835,10 @@ const xs_val *xs_list_get(const xs_list *list, int num)
831 if (num < 0) 835 if (num < 0)
832 num = xs_list_len(list) + num; 836 num = xs_list_len(list) + num;
833 837
834 int c = 0, ct = 0; 838 int c = 0;
835 const xs_val *v; 839 const xs_val *v;
836 840
837 while (xs_list_next(list, &v, &ct)) { 841 xs_list_foreach(list, v) {
838 if (c == num) 842 if (c == num)
839 return v; 843 return v;
840 844
@@ -922,11 +926,10 @@ int xs_list_in(const xs_list *list, const xs_val *val)
922 XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST); 926 XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
923 927
924 int n = 0; 928 int n = 0;
925 int ct = 0;
926 const xs_val *v; 929 const xs_val *v;
927 int sz = xs_size(val); 930 int sz = xs_size(val);
928 931
929 while (xs_list_next(list, &v, &ct)) { 932 xs_list_foreach(list, v) {
930 if (sz == xs_size(v) && memcmp(val, v, sz) == 0) 933 if (sz == xs_size(v) && memcmp(val, v, sz) == 0)
931 return n; 934 return n;
932 935
@@ -945,11 +948,10 @@ xs_str *xs_join(const xs_list *list, const char *sep)
945 xs_str *s = NULL; 948 xs_str *s = NULL;
946 const xs_val *v; 949 const xs_val *v;
947 int c = 0; 950 int c = 0;
948 int ct = 0;
949 int offset = 0; 951 int offset = 0;
950 int ssz = strlen(sep); 952 int ssz = strlen(sep);
951 953
952 while (xs_list_next(list, &v, &ct)) { 954 xs_list_foreach(list, v) {
953 /* refuse to join non-string values */ 955 /* refuse to join non-string values */
954 if (xs_type(v) == XSTYPE_STRING) { 956 if (xs_type(v) == XSTYPE_STRING) {
955 int sz; 957 int sz;
@@ -1277,9 +1279,8 @@ xs_dict *xs_dict_gc(const xs_dict *dict)
1277 xs_dict *nd = xs_dict_new(); 1279 xs_dict *nd = xs_dict_new();
1278 const xs_str *k; 1280 const xs_str *k;
1279 const xs_val *v; 1281 const xs_val *v;
1280 int c = 0;
1281 1282
1282 while (xs_dict_next(dict, &k, &v, &c)) { 1283 xs_dict_foreach(dict, k, v) {
1283 if (xs_type(v) == XSTYPE_DICT) { 1284 if (xs_type(v) == XSTYPE_DICT) {
1284 xs *sd = xs_dict_gc(v); 1285 xs *sd = xs_dict_gc(v);
1285 nd = xs_dict_set(nd, k, sd); 1286 nd = xs_dict_set(nd, k, sd);