Browse Source

Backport from xs.

default 7 months ago
parent
commit
0218e964b0
9 changed files with 21 additions and 28 deletions
  1. 11 10
      xs.h
  2. 1 2
      xs_curl.h
  3. 1 2
      xs_fcgi.h
  4. 1 2
      xs_httpd.h
  5. 2 3
      xs_json.h
  6. 2 4
      xs_regex.h
  7. 1 2
      xs_set.h
  8. 1 2
      xs_url.h
  9. 1 1
      xs_version.h

+ 11 - 10
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);

+ 1 - 2
xs_curl.h

@@ -146,8 +146,7 @@ xs_dict *xs_http_request(const char *method, const char *url,
     }
 
     /* fill the request headers */
-    int c = 0;
-    while (xs_dict_next(headers, &k, &v, &c)) {
+    xs_dict_foreach(headers, k, v) {
         xs *h = xs_fmt("%s: %s", k, v);
 
         list = curl_slist_append(list, h);

+ 1 - 2
xs_fcgi.h

@@ -306,8 +306,7 @@ void xs_fcgi_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b
         out = xs_str_cat(out, s1);
     }
 
-    int c = 0;
-    while (xs_dict_next(headers, &k, &v, &c)) {
+    xs_dict_foreach(headers, k, v) {
         xs *s1 = xs_fmt("%s: %s\r\n", k, v);
         out = xs_str_cat(out, s1);
     }

+ 1 - 2
xs_httpd.h

@@ -105,8 +105,7 @@ void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *he
     proto = xs_fmt("HTTP/1.1 %d %s", status, status_text);
     fprintf(f, "%s\r\n", proto);
 
-    int c = 0;
-    while (xs_dict_next(headers, &k, &v, &c)) {
+    xs_dict_foreach(headers, k, v) {
         fprintf(f, "%s: %s\r\n", k, v);
     }
 

+ 2 - 3
xs_json.h

@@ -75,7 +75,6 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f)
 /* dumps partial data as JSON */
 {
     int c = 0;
-    int ct = 0;
     const xs_val *v;
 
     switch (xs_type(data)) {
@@ -98,7 +97,7 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f)
     case XSTYPE_LIST:
         fputc('[', f);
 
-        while (xs_list_next(data, &v, &ct)) {
+        xs_list_foreach(data, v) {
             if (c != 0)
                 fputc(',', f);
 
@@ -118,7 +117,7 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f)
 
         const xs_str *k;
 
-        while (xs_dict_next(data, &k, &v, &ct)) {
+        xs_dict_foreach(data, k, v) {
             if (c != 0)
                 fputc(',', f);
 

+ 2 - 4
xs_regex.h

@@ -71,13 +71,12 @@ xs_list *xs_regex_select_n(const char *str, const char *rx, int count)
     xs *split = NULL;
     const xs_val *v;
     int n = 0;
-    int c = 0;
 
     /* split */
     split = xs_regex_split_n(str, rx, count);
 
     /* now iterate to get only the 'separators' (odd ones) */
-    while (xs_list_next(split, &v, &c)) {
+    xs_list_foreach(split, v) {
         if (n & 0x1)
             list = xs_list_append(list, v);
 
@@ -96,10 +95,9 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c
     xs *split = xs_regex_split_n(str, rx, count);
     const xs_val *v;
     int n = 0;
-    int c = 0;
     int pholder = !!strchr(rep, '&');
 
-    while (xs_list_next(split, &v, &c)) {
+    xs_list_foreach(split, v) {
         if (n & 0x1) {
             if (pholder) {
                 /* rep has a placeholder; process char by char */

+ 1 - 2
xs_set.h

@@ -95,8 +95,7 @@ int xs_set_add(xs_set *s, const xs_val *data)
         memset(s->hash, '\0', s->elems * sizeof(int));
 
         /* add the list elements back */
-        int ct = 0;
-        while (xs_list_next(s->list, &v, &ct))
+        xs_list_foreach(s->list, v)
             _store_hash(s, v, v - s->list);
     }
 

+ 1 - 2
xs_url.h

@@ -50,10 +50,9 @@ xs_dict *xs_url_vars(const char *str)
         /* split by arguments */
         xs *args = xs_split(str, "&");
 
-        int ct = 0;
         const xs_val *v;
 
-        while (xs_list_next(args, &v, &ct)) {
+        xs_list_foreach(args, v) {
             xs *kv = xs_split_n(v, "=", 1);
 
             if (xs_list_len(kv) == 2) {

+ 1 - 1
xs_version.h

@@ -1 +1 @@
-/* cc9ebd36ae640e4701277327fbba9996143076f6 2024-08-23T17:17:08+02:00 */
+/* 2a3ecc6aef531366cfd45cbf19e34a15f83f69f8 2024-08-30T18:33:51+02:00 */