diff options
| author | 2024-08-30 19:10:26 +0200 | |
|---|---|---|
| committer | 2024-08-30 19:10:26 +0200 | |
| commit | 0218e964b0d73340c8d0c5d9e37991359d7c4be9 (patch) | |
| tree | 9cfc1650abf8780be52dc219e37a311244a294e2 | |
| parent | timeline_del() also deletes from the pinned and bookmark caches. (diff) | |
| download | snac2-0218e964b0d73340c8d0c5d9e37991359d7c4be9.tar.gz snac2-0218e964b0d73340c8d0c5d9e37991359d7c4be9.tar.xz snac2-0218e964b0d73340c8d0c5d9e37991359d7c4be9.zip | |
Backport from xs.
| -rw-r--r-- | xs.h | 21 | ||||
| -rw-r--r-- | xs_curl.h | 3 | ||||
| -rw-r--r-- | xs_fcgi.h | 3 | ||||
| -rw-r--r-- | xs_httpd.h | 3 | ||||
| -rw-r--r-- | xs_json.h | 5 | ||||
| -rw-r--r-- | xs_regex.h | 6 | ||||
| -rw-r--r-- | xs_set.h | 3 | ||||
| -rw-r--r-- | xs_url.h | 3 | ||||
| -rw-r--r-- | xs_version.h | 2 |
9 files changed, 21 insertions, 28 deletions
| @@ -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 | ||
| 163 | void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func) | 167 | 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) | |||
| 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); |
| @@ -146,8 +146,7 @@ xs_dict *xs_http_request(const char *method, const char *url, | |||
| 146 | } | 146 | } |
| 147 | 147 | ||
| 148 | /* fill the request headers */ | 148 | /* fill the request headers */ |
| 149 | int c = 0; | 149 | xs_dict_foreach(headers, k, v) { |
| 150 | while (xs_dict_next(headers, &k, &v, &c)) { | ||
| 151 | xs *h = xs_fmt("%s: %s", k, v); | 150 | xs *h = xs_fmt("%s: %s", k, v); |
| 152 | 151 | ||
| 153 | list = curl_slist_append(list, h); | 152 | list = curl_slist_append(list, h); |
| @@ -306,8 +306,7 @@ void xs_fcgi_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b | |||
| 306 | out = xs_str_cat(out, s1); | 306 | out = xs_str_cat(out, s1); |
| 307 | } | 307 | } |
| 308 | 308 | ||
| 309 | int c = 0; | 309 | xs_dict_foreach(headers, k, v) { |
| 310 | while (xs_dict_next(headers, &k, &v, &c)) { | ||
| 311 | xs *s1 = xs_fmt("%s: %s\r\n", k, v); | 310 | xs *s1 = xs_fmt("%s: %s\r\n", k, v); |
| 312 | out = xs_str_cat(out, s1); | 311 | out = xs_str_cat(out, s1); |
| 313 | } | 312 | } |
| @@ -105,8 +105,7 @@ void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *he | |||
| 105 | proto = xs_fmt("HTTP/1.1 %d %s", status, status_text); | 105 | proto = xs_fmt("HTTP/1.1 %d %s", status, status_text); |
| 106 | fprintf(f, "%s\r\n", proto); | 106 | fprintf(f, "%s\r\n", proto); |
| 107 | 107 | ||
| 108 | int c = 0; | 108 | xs_dict_foreach(headers, k, v) { |
| 109 | while (xs_dict_next(headers, &k, &v, &c)) { | ||
| 110 | fprintf(f, "%s: %s\r\n", k, v); | 109 | fprintf(f, "%s: %s\r\n", k, v); |
| 111 | } | 110 | } |
| 112 | 111 | ||
| @@ -75,7 +75,6 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f) | |||
| 75 | /* dumps partial data as JSON */ | 75 | /* dumps partial data as JSON */ |
| 76 | { | 76 | { |
| 77 | int c = 0; | 77 | int c = 0; |
| 78 | int ct = 0; | ||
| 79 | const xs_val *v; | 78 | const xs_val *v; |
| 80 | 79 | ||
| 81 | switch (xs_type(data)) { | 80 | switch (xs_type(data)) { |
| @@ -98,7 +97,7 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f) | |||
| 98 | case XSTYPE_LIST: | 97 | case XSTYPE_LIST: |
| 99 | fputc('[', f); | 98 | fputc('[', f); |
| 100 | 99 | ||
| 101 | while (xs_list_next(data, &v, &ct)) { | 100 | xs_list_foreach(data, v) { |
| 102 | if (c != 0) | 101 | if (c != 0) |
| 103 | fputc(',', f); | 102 | fputc(',', f); |
| 104 | 103 | ||
| @@ -118,7 +117,7 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f) | |||
| 118 | 117 | ||
| 119 | const xs_str *k; | 118 | const xs_str *k; |
| 120 | 119 | ||
| 121 | while (xs_dict_next(data, &k, &v, &ct)) { | 120 | xs_dict_foreach(data, k, v) { |
| 122 | if (c != 0) | 121 | if (c != 0) |
| 123 | fputc(',', f); | 122 | fputc(',', f); |
| 124 | 123 | ||
| @@ -71,13 +71,12 @@ xs_list *xs_regex_select_n(const char *str, const char *rx, int count) | |||
| 71 | xs *split = NULL; | 71 | xs *split = NULL; |
| 72 | const xs_val *v; | 72 | const xs_val *v; |
| 73 | int n = 0; | 73 | int n = 0; |
| 74 | int c = 0; | ||
| 75 | 74 | ||
| 76 | /* split */ | 75 | /* split */ |
| 77 | split = xs_regex_split_n(str, rx, count); | 76 | split = xs_regex_split_n(str, rx, count); |
| 78 | 77 | ||
| 79 | /* now iterate to get only the 'separators' (odd ones) */ | 78 | /* now iterate to get only the 'separators' (odd ones) */ |
| 80 | while (xs_list_next(split, &v, &c)) { | 79 | xs_list_foreach(split, v) { |
| 81 | if (n & 0x1) | 80 | if (n & 0x1) |
| 82 | list = xs_list_append(list, v); | 81 | list = xs_list_append(list, v); |
| 83 | 82 | ||
| @@ -96,10 +95,9 @@ xs_list *xs_regex_replace_in(xs_str *str, const char *rx, const char *rep, int c | |||
| 96 | xs *split = xs_regex_split_n(str, rx, count); | 95 | xs *split = xs_regex_split_n(str, rx, count); |
| 97 | const xs_val *v; | 96 | const xs_val *v; |
| 98 | int n = 0; | 97 | int n = 0; |
| 99 | int c = 0; | ||
| 100 | int pholder = !!strchr(rep, '&'); | 98 | int pholder = !!strchr(rep, '&'); |
| 101 | 99 | ||
| 102 | while (xs_list_next(split, &v, &c)) { | 100 | xs_list_foreach(split, v) { |
| 103 | if (n & 0x1) { | 101 | if (n & 0x1) { |
| 104 | if (pholder) { | 102 | if (pholder) { |
| 105 | /* rep has a placeholder; process char by char */ | 103 | /* rep has a placeholder; process char by char */ |
| @@ -95,8 +95,7 @@ int xs_set_add(xs_set *s, const xs_val *data) | |||
| 95 | memset(s->hash, '\0', s->elems * sizeof(int)); | 95 | memset(s->hash, '\0', s->elems * sizeof(int)); |
| 96 | 96 | ||
| 97 | /* add the list elements back */ | 97 | /* add the list elements back */ |
| 98 | int ct = 0; | 98 | xs_list_foreach(s->list, v) |
| 99 | while (xs_list_next(s->list, &v, &ct)) | ||
| 100 | _store_hash(s, v, v - s->list); | 99 | _store_hash(s, v, v - s->list); |
| 101 | } | 100 | } |
| 102 | 101 | ||
| @@ -50,10 +50,9 @@ xs_dict *xs_url_vars(const char *str) | |||
| 50 | /* split by arguments */ | 50 | /* split by arguments */ |
| 51 | xs *args = xs_split(str, "&"); | 51 | xs *args = xs_split(str, "&"); |
| 52 | 52 | ||
| 53 | int ct = 0; | ||
| 54 | const xs_val *v; | 53 | const xs_val *v; |
| 55 | 54 | ||
| 56 | while (xs_list_next(args, &v, &ct)) { | 55 | xs_list_foreach(args, v) { |
| 57 | xs *kv = xs_split_n(v, "=", 1); | 56 | xs *kv = xs_split_n(v, "=", 1); |
| 58 | 57 | ||
| 59 | if (xs_list_len(kv) == 2) { | 58 | if (xs_list_len(kv) == 2) { |
diff --git a/xs_version.h b/xs_version.h index ce88558..a5559d5 100644 --- a/xs_version.h +++ b/xs_version.h | |||
| @@ -1 +1 @@ | |||
| /* cc9ebd36ae640e4701277327fbba9996143076f6 2024-08-23T17:17:08+02:00 */ | /* 2a3ecc6aef531366cfd45cbf19e34a15f83f69f8 2024-08-30T18:33:51+02:00 */ | ||