mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-12-12 11:53:37 +00:00
Backport from xs.
This commit is contained in:
parent
bff33fc2ed
commit
0218e964b0
9 changed files with 21 additions and 28 deletions
21
xs.h
21
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_true(v) (xs_type((v)) == XSTYPE_TRUE)
|
||||||
#define xs_is_false(v) (xs_type((v)) == XSTYPE_FALSE)
|
#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
|
#ifdef XS_IMPLEMENTATION
|
||||||
|
|
||||||
void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func)
|
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);
|
XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
|
||||||
|
|
||||||
int c = 0, ct = 0;
|
int c = 0;
|
||||||
const xs_val *v;
|
const xs_val *v;
|
||||||
|
|
||||||
while (xs_list_next(list, &v, &ct))
|
xs_list_foreach(list, v)
|
||||||
c++;
|
c++;
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
|
@ -831,10 +835,10 @@ const xs_val *xs_list_get(const xs_list *list, int num)
|
||||||
if (num < 0)
|
if (num < 0)
|
||||||
num = xs_list_len(list) + num;
|
num = xs_list_len(list) + num;
|
||||||
|
|
||||||
int c = 0, ct = 0;
|
int c = 0;
|
||||||
const xs_val *v;
|
const xs_val *v;
|
||||||
|
|
||||||
while (xs_list_next(list, &v, &ct)) {
|
xs_list_foreach(list, v) {
|
||||||
if (c == num)
|
if (c == num)
|
||||||
return v;
|
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);
|
XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
|
||||||
|
|
||||||
int n = 0;
|
int n = 0;
|
||||||
int ct = 0;
|
|
||||||
const xs_val *v;
|
const xs_val *v;
|
||||||
int sz = xs_size(val);
|
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)
|
if (sz == xs_size(v) && memcmp(val, v, sz) == 0)
|
||||||
return n;
|
return n;
|
||||||
|
|
||||||
|
@ -945,11 +948,10 @@ xs_str *xs_join(const xs_list *list, const char *sep)
|
||||||
xs_str *s = NULL;
|
xs_str *s = NULL;
|
||||||
const xs_val *v;
|
const xs_val *v;
|
||||||
int c = 0;
|
int c = 0;
|
||||||
int ct = 0;
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
int ssz = strlen(sep);
|
int ssz = strlen(sep);
|
||||||
|
|
||||||
while (xs_list_next(list, &v, &ct)) {
|
xs_list_foreach(list, v) {
|
||||||
/* refuse to join non-string values */
|
/* refuse to join non-string values */
|
||||||
if (xs_type(v) == XSTYPE_STRING) {
|
if (xs_type(v) == XSTYPE_STRING) {
|
||||||
int sz;
|
int sz;
|
||||||
|
@ -1277,9 +1279,8 @@ xs_dict *xs_dict_gc(const xs_dict *dict)
|
||||||
xs_dict *nd = xs_dict_new();
|
xs_dict *nd = xs_dict_new();
|
||||||
const xs_str *k;
|
const xs_str *k;
|
||||||
const xs_val *v;
|
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) {
|
if (xs_type(v) == XSTYPE_DICT) {
|
||||||
xs *sd = xs_dict_gc(v);
|
xs *sd = xs_dict_gc(v);
|
||||||
nd = xs_dict_set(nd, k, sd);
|
nd = xs_dict_set(nd, k, sd);
|
||||||
|
|
|
@ -146,8 +146,7 @@ xs_dict *xs_http_request(const char *method, const char *url,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* fill the request headers */
|
/* fill the request headers */
|
||||||
int c = 0;
|
xs_dict_foreach(headers, k, v) {
|
||||||
while (xs_dict_next(headers, &k, &v, &c)) {
|
|
||||||
xs *h = xs_fmt("%s: %s", k, v);
|
xs *h = xs_fmt("%s: %s", k, v);
|
||||||
|
|
||||||
list = curl_slist_append(list, h);
|
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
|
||||||
out = xs_str_cat(out, s1);
|
out = xs_str_cat(out, s1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int c = 0;
|
xs_dict_foreach(headers, k, v) {
|
||||||
while (xs_dict_next(headers, &k, &v, &c)) {
|
|
||||||
xs *s1 = xs_fmt("%s: %s\r\n", k, v);
|
xs *s1 = xs_fmt("%s: %s\r\n", k, v);
|
||||||
out = xs_str_cat(out, s1);
|
out = xs_str_cat(out, s1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
proto = xs_fmt("HTTP/1.1 %d %s", status, status_text);
|
||||||
fprintf(f, "%s\r\n", proto);
|
fprintf(f, "%s\r\n", proto);
|
||||||
|
|
||||||
int c = 0;
|
xs_dict_foreach(headers, k, v) {
|
||||||
while (xs_dict_next(headers, &k, &v, &c)) {
|
|
||||||
fprintf(f, "%s: %s\r\n", k, v);
|
fprintf(f, "%s: %s\r\n", k, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,7 +75,6 @@ static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f)
|
||||||
/* dumps partial data as JSON */
|
/* dumps partial data as JSON */
|
||||||
{
|
{
|
||||||
int c = 0;
|
int c = 0;
|
||||||
int ct = 0;
|
|
||||||
const xs_val *v;
|
const xs_val *v;
|
||||||
|
|
||||||
switch (xs_type(data)) {
|
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:
|
case XSTYPE_LIST:
|
||||||
fputc('[', f);
|
fputc('[', f);
|
||||||
|
|
||||||
while (xs_list_next(data, &v, &ct)) {
|
xs_list_foreach(data, v) {
|
||||||
if (c != 0)
|
if (c != 0)
|
||||||
fputc(',', f);
|
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;
|
const xs_str *k;
|
||||||
|
|
||||||
while (xs_dict_next(data, &k, &v, &ct)) {
|
xs_dict_foreach(data, k, v) {
|
||||||
if (c != 0)
|
if (c != 0)
|
||||||
fputc(',', f);
|
fputc(',', f);
|
||||||
|
|
||||||
|
|
|
@ -71,13 +71,12 @@ xs_list *xs_regex_select_n(const char *str, const char *rx, int count)
|
||||||
xs *split = NULL;
|
xs *split = NULL;
|
||||||
const xs_val *v;
|
const xs_val *v;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
int c = 0;
|
|
||||||
|
|
||||||
/* split */
|
/* split */
|
||||||
split = xs_regex_split_n(str, rx, count);
|
split = xs_regex_split_n(str, rx, count);
|
||||||
|
|
||||||
/* now iterate to get only the 'separators' (odd ones) */
|
/* now iterate to get only the 'separators' (odd ones) */
|
||||||
while (xs_list_next(split, &v, &c)) {
|
xs_list_foreach(split, v) {
|
||||||
if (n & 0x1)
|
if (n & 0x1)
|
||||||
list = xs_list_append(list, v);
|
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);
|
xs *split = xs_regex_split_n(str, rx, count);
|
||||||
const xs_val *v;
|
const xs_val *v;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
int c = 0;
|
|
||||||
int pholder = !!strchr(rep, '&');
|
int pholder = !!strchr(rep, '&');
|
||||||
|
|
||||||
while (xs_list_next(split, &v, &c)) {
|
xs_list_foreach(split, v) {
|
||||||
if (n & 0x1) {
|
if (n & 0x1) {
|
||||||
if (pholder) {
|
if (pholder) {
|
||||||
/* rep has a placeholder; process char by char */
|
/* rep has a placeholder; process char by char */
|
||||||
|
|
3
xs_set.h
3
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));
|
memset(s->hash, '\0', s->elems * sizeof(int));
|
||||||
|
|
||||||
/* add the list elements back */
|
/* add the list elements back */
|
||||||
int ct = 0;
|
xs_list_foreach(s->list, v)
|
||||||
while (xs_list_next(s->list, &v, &ct))
|
|
||||||
_store_hash(s, v, v - s->list);
|
_store_hash(s, v, v - s->list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
3
xs_url.h
3
xs_url.h
|
@ -50,10 +50,9 @@ xs_dict *xs_url_vars(const char *str)
|
||||||
/* split by arguments */
|
/* split by arguments */
|
||||||
xs *args = xs_split(str, "&");
|
xs *args = xs_split(str, "&");
|
||||||
|
|
||||||
int ct = 0;
|
|
||||||
const xs_val *v;
|
const xs_val *v;
|
||||||
|
|
||||||
while (xs_list_next(args, &v, &ct)) {
|
xs_list_foreach(args, v) {
|
||||||
xs *kv = xs_split_n(v, "=", 1);
|
xs *kv = xs_split_n(v, "=", 1);
|
||||||
|
|
||||||
if (xs_list_len(kv) == 2) {
|
if (xs_list_len(kv) == 2) {
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
/* cc9ebd36ae640e4701277327fbba9996143076f6 2024-08-23T17:17:08+02:00 */
|
/* 2a3ecc6aef531366cfd45cbf19e34a15f83f69f8 2024-08-30T18:33:51+02:00 */
|
||||||
|
|
Loading…
Reference in a new issue