summaryrefslogtreecommitdiff
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
parentbff33fc2eda701ff5d6cd8433c15dfd9aca768f7 (diff)
Backport from xs.
-rw-r--r--xs.h21
-rw-r--r--xs_curl.h3
-rw-r--r--xs_fcgi.h3
-rw-r--r--xs_httpd.h3
-rw-r--r--xs_json.h5
-rw-r--r--xs_regex.h6
-rw-r--r--xs_set.h3
-rw-r--r--xs_url.h3
-rw-r--r--xs_version.h2
9 files changed, 21 insertions, 28 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);
diff --git a/xs_curl.h b/xs_curl.h
index 215db7f..9b4c229 100644
--- a/xs_curl.h
+++ b/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);
diff --git a/xs_fcgi.h b/xs_fcgi.h
index a1433a2..0dbd895 100644
--- a/xs_fcgi.h
+++ b/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);
}
diff --git a/xs_httpd.h b/xs_httpd.h
index d080b39..1782487 100644
--- a/xs_httpd.h
+++ b/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);
}
diff --git a/xs_json.h b/xs_json.h
index 3a91de9..de9600f 100644
--- a/xs_json.h
+++ b/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);
diff --git a/xs_regex.h b/xs_regex.h
index d8d2d1d..3ba504b 100644
--- a/xs_regex.h
+++ b/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 */
diff --git a/xs_set.h b/xs_set.h
index 6ab7f0b..94c2b84 100644
--- a/xs_set.h
+++ b/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);
}
diff --git a/xs_url.h b/xs_url.h
index 56e0f53..efc883b 100644
--- a/xs_url.h
+++ b/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) {
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 */