commit 0218e96

default  ·  2024-08-30 17:10:26 +0000 UTC
parent bff33fc
Backport from xs.
9 files changed,  +21, -28
M xs.h
M xs.h
+11, -10
 1@@ -158,6 +158,10 @@ unsigned int xs_hash_func(const char *data, int size);
 2 #define xs_is_true(v) (xs_type((v)) == XSTYPE_TRUE)
 3 #define xs_is_false(v) (xs_type((v)) == XSTYPE_FALSE)
 4 
 5+#define xs_list_foreach(l, v) for (int ct_##__LINE__ = 0; xs_list_next(l, &v, &ct_##__LINE__); )
 6+#define xs_dict_foreach(l, k, v) for (int ct_##__LINE__ = 0; xs_dict_next(l, &k, &v, &ct_##__LINE__); )
 7+
 8+
 9 #ifdef XS_IMPLEMENTATION
10 
11 void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func)
12@@ -813,10 +817,10 @@ int xs_list_len(const xs_list *list)
13 {
14     XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
15 
16-    int c = 0, ct = 0;
17+    int c = 0;
18     const xs_val *v;
19 
20-    while (xs_list_next(list, &v, &ct))
21+    xs_list_foreach(list, v)
22         c++;
23 
24     return c;
25@@ -831,10 +835,10 @@ const xs_val *xs_list_get(const xs_list *list, int num)
26     if (num < 0)
27         num = xs_list_len(list) + num;
28 
29-    int c = 0, ct = 0;
30+    int c = 0;
31     const xs_val *v;
32 
33-    while (xs_list_next(list, &v, &ct)) {
34+    xs_list_foreach(list, v) {
35         if (c == num)
36             return v;
37 
38@@ -922,11 +926,10 @@ int xs_list_in(const xs_list *list, const xs_val *val)
39     XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
40 
41     int n = 0;
42-    int ct = 0;
43     const xs_val *v;
44     int sz = xs_size(val);
45 
46-    while (xs_list_next(list, &v, &ct)) {
47+    xs_list_foreach(list, v) {
48         if (sz == xs_size(v) && memcmp(val, v, sz) == 0)
49             return n;
50 
51@@ -945,11 +948,10 @@ xs_str *xs_join(const xs_list *list, const char *sep)
52     xs_str *s = NULL;
53     const xs_val *v;
54     int c = 0;
55-    int ct = 0;
56     int offset = 0;
57     int ssz = strlen(sep);
58 
59-    while (xs_list_next(list, &v, &ct)) {
60+    xs_list_foreach(list, v) {
61         /* refuse to join non-string values */
62         if (xs_type(v) == XSTYPE_STRING) {
63             int sz;
64@@ -1277,9 +1279,8 @@ xs_dict *xs_dict_gc(const xs_dict *dict)
65     xs_dict *nd = xs_dict_new();
66     const xs_str *k;
67     const xs_val *v;
68-    int c = 0;
69 
70-    while (xs_dict_next(dict, &k, &v, &c)) {
71+    xs_dict_foreach(dict, k, v) {
72         if (xs_type(v) == XSTYPE_DICT) {
73             xs *sd = xs_dict_gc(v);
74             nd = xs_dict_set(nd, k, sd);
+1, -2
 1@@ -146,8 +146,7 @@ xs_dict *xs_http_request(const char *method, const char *url,
 2     }
 3 
 4     /* fill the request headers */
 5-    int c = 0;
 6-    while (xs_dict_next(headers, &k, &v, &c)) {
 7+    xs_dict_foreach(headers, k, v) {
 8         xs *h = xs_fmt("%s: %s", k, v);
 9 
10         list = curl_slist_append(list, h);
+1, -2
 1@@ -306,8 +306,7 @@ void xs_fcgi_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b
 2         out = xs_str_cat(out, s1);
 3     }
 4 
 5-    int c = 0;
 6-    while (xs_dict_next(headers, &k, &v, &c)) {
 7+    xs_dict_foreach(headers, k, v) {
 8         xs *s1 = xs_fmt("%s: %s\r\n", k, v);
 9         out = xs_str_cat(out, s1);
10     }
+1, -2
 1@@ -105,8 +105,7 @@ void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *he
 2     proto = xs_fmt("HTTP/1.1 %d %s", status, status_text);
 3     fprintf(f, "%s\r\n", proto);
 4 
 5-    int c = 0;
 6-    while (xs_dict_next(headers, &k, &v, &c)) {
 7+    xs_dict_foreach(headers, k, v) {
 8         fprintf(f, "%s: %s\r\n", k, v);
 9     }
10 
+2, -3
 1@@ -75,7 +75,6 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f)
 2 /* dumps partial data as JSON */
 3 {
 4     int c = 0;
 5-    int ct = 0;
 6     const xs_val *v;
 7 
 8     switch (xs_type(data)) {
 9@@ -98,7 +97,7 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f)
10     case XSTYPE_LIST:
11         fputc('[', f);
12 
13-        while (xs_list_next(data, &v, &ct)) {
14+        xs_list_foreach(data, v) {
15             if (c != 0)
16                 fputc(',', f);
17 
18@@ -118,7 +117,7 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f)
19 
20         const xs_str *k;
21 
22-        while (xs_dict_next(data, &k, &v, &ct)) {
23+        xs_dict_foreach(data, k, v) {
24             if (c != 0)
25                 fputc(',', f);
26 
+2, -4
 1@@ -71,13 +71,12 @@ xs_list *xs_regex_select_n(const char *str, const char *rx, int count)
 2     xs *split = NULL;
 3     const xs_val *v;
 4     int n = 0;
 5-    int c = 0;
 6 
 7     /* split */
 8     split = xs_regex_split_n(str, rx, count);
 9 
10     /* now iterate to get only the 'separators' (odd ones) */
11-    while (xs_list_next(split, &v, &c)) {
12+    xs_list_foreach(split, v) {
13         if (n & 0x1)
14             list = xs_list_append(list, v);
15 
16@@ -96,10 +95,9 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c
17     xs *split = xs_regex_split_n(str, rx, count);
18     const xs_val *v;
19     int n = 0;
20-    int c = 0;
21     int pholder = !!strchr(rep, '&');
22 
23-    while (xs_list_next(split, &v, &c)) {
24+    xs_list_foreach(split, v) {
25         if (n & 0x1) {
26             if (pholder) {
27                 /* rep has a placeholder; process char by char */
+1, -2
 1@@ -95,8 +95,7 @@ int xs_set_add(xs_set *s, const xs_val *data)
 2         memset(s->hash, '\0', s->elems * sizeof(int));
 3 
 4         /* add the list elements back */
 5-        int ct = 0;
 6-        while (xs_list_next(s->list, &v, &ct))
 7+        xs_list_foreach(s->list, v)
 8             _store_hash(s, v, v - s->list);
 9     }
10 
+1, -2
 1@@ -50,10 +50,9 @@ xs_dict *xs_url_vars(const char *str)
 2         /* split by arguments */
 3         xs *args = xs_split(str, "&");
 4 
 5-        int ct = 0;
 6         const xs_val *v;
 7 
 8-        while (xs_list_next(args, &v, &ct)) {
 9+        xs_list_foreach(args, v) {
10             xs *kv = xs_split_n(v, "=", 1);
11 
12             if (xs_list_len(kv) == 2) {
+1, -1
1@@ -1 +1 @@
2-/* cc9ebd36ae640e4701277327fbba9996143076f6 2024-08-23T17:17:08+02:00 */
3+/* 2a3ecc6aef531366cfd45cbf19e34a15f83f69f8 2024-08-30T18:33:51+02:00 */