From 7aa5250eb8c68472e476faf8399e98f2ba9a9c3b Mon Sep 17 00:00:00 2001 From: default Date: Fri, 15 Mar 2024 04:10:11 +0100 Subject: [PATCH] Backport from xs. --- activitypub.c | 4 +- html.c | 28 ++++++------ httpd.c | 2 +- mastoapi.c | 118 +++++++++++++++++++++++++------------------------- xs.h | 99 ++++++++++++++++++++++++++---------------- xs_version.h | 2 +- 6 files changed, 138 insertions(+), 115 deletions(-) diff --git a/activitypub.c b/activitypub.c index fae0c15..eb1552d 100644 --- a/activitypub.c +++ b/activitypub.c @@ -1074,7 +1074,7 @@ xs_dict *msg_collection(snac *snac, char *id) msg = xs_dict_append(msg, "attributedTo", snac->actor); msg = xs_dict_append(msg, "orderedItems", ol); - msg = xs_dict_append(msg, "totalItems", xs_stock_0); + msg = xs_dict_append(msg, "totalItems", xs_stock(0)); return msg; } @@ -1610,7 +1610,7 @@ int update_question(snac *user, const char *id) const char *name = xs_dict_get(v, "name"); if (name) { lopts = xs_list_append(lopts, name); - rcnt = xs_dict_set(rcnt, name, xs_stock_0); + rcnt = xs_dict_set(rcnt, name, xs_stock(0)); } } diff --git a/html.c b/html.c index da37558..667b795 100644 --- a/html.c +++ b/html.c @@ -774,7 +774,7 @@ static xs_html *html_user_body(snac *user, int read_only) xs_dict *val_links = user->links; if (xs_is_null(val_links)) - val_links = xs_stock_dict; + val_links = xs_stock(XSTYPE_DICT); xs_html *snac_metadata = xs_html_tag("div", xs_html_attr("class", "snac-metadata")); @@ -852,8 +852,8 @@ xs_html *html_top_controls(snac *snac) "new_post_div", "new_post_form", L("What's on your mind?"), "", NULL, NULL, - xs_stock_false, "", - xs_stock_false, NULL, + xs_stock(XSTYPE_FALSE), "", + xs_stock(XSTYPE_FALSE), NULL, NULL, 1), /** operations **/ @@ -1285,7 +1285,7 @@ xs_html *html_entry_controls(snac *snac, char *actor, const xs_dict *msg, const "", prev_src, id, NULL, xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"), - xs_stock_false, redir, + xs_stock(XSTYPE_FALSE), redir, NULL, 0)), xs_html_tag("p", NULL)); } @@ -1304,7 +1304,7 @@ xs_html *html_entry_controls(snac *snac, char *actor, const xs_dict *msg, const "", ct, NULL, NULL, xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"), - xs_stock_false, redir, + xs_stock(XSTYPE_FALSE), redir, id, 0)), xs_html_tag("p", NULL)); } @@ -2181,8 +2181,8 @@ xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t) dm_div_id, dm_form_id, "", "", NULL, actor_id, - xs_stock_false, "", - xs_stock_false, NULL, + xs_stock(XSTYPE_FALSE), "", + xs_stock(XSTYPE_FALSE), NULL, NULL, 0), xs_html_tag("p", NULL)); @@ -2802,7 +2802,7 @@ int html_post_handler(const xs_dict *req, const char *q_path, msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv); if (sensitive != NULL) { - msg = xs_dict_set(msg, "sensitive", xs_stock_true); + msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE)); msg = xs_dict_set(msg, "summary", xs_is_null(summary) ? "..." : summary); } @@ -3036,17 +3036,17 @@ int html_post_handler(const xs_dict *req, const char *q_path, snac.config = xs_dict_set(snac.config, "purge_days", days); } if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0) - snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock_true); + snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_TRUE)); else - snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock_false); + snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_FALSE)); if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0) - snac.config = xs_dict_set(snac.config, "bot", xs_stock_true); + snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_TRUE)); else - snac.config = xs_dict_set(snac.config, "bot", xs_stock_false); + snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_FALSE)); if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0) - snac.config = xs_dict_set(snac.config, "private", xs_stock_true); + snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_TRUE)); else - snac.config = xs_dict_set(snac.config, "private", xs_stock_false); + snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_FALSE)); if ((v = xs_dict_get(p_vars, "metadata")) != NULL) { /* split the metadata and store it as a dict */ xs_dict *md = xs_dict_new(); diff --git a/httpd.c b/httpd.c index d74642f..51ed1a2 100644 --- a/httpd.c +++ b/httpd.c @@ -814,7 +814,7 @@ void httpd(void) /* send as many exit jobs as working threads */ for (n = 1; n < p_state->n_threads; n++) - job_post(xs_stock_false, 0); + job_post(xs_stock(XSTYPE_FALSE), 0); /* wait for all the threads to exit */ for (n = 0; n < p_state->n_threads; n++) diff --git a/mastoapi.c b/mastoapi.c index cc8e144..0ec1429 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -522,12 +522,12 @@ xs_dict *mastoapi_account(const xs_dict *actor) acct = xs_dict_append(acct, "id", acct_md5); acct = xs_dict_append(acct, "username", prefu); acct = xs_dict_append(acct, "display_name", display_name); - acct = xs_dict_append(acct, "discoverable", xs_stock_true); - acct = xs_dict_append(acct, "group", xs_stock_false); - acct = xs_dict_append(acct, "hide_collections", xs_stock_false); - acct = xs_dict_append(acct, "indexable", xs_stock_true); - acct = xs_dict_append(acct, "noindex", xs_stock_false); - acct = xs_dict_append(acct, "roles", xs_stock_list); + acct = xs_dict_append(acct, "discoverable", xs_stock(XSTYPE_TRUE)); + acct = xs_dict_append(acct, "group", xs_stock(XSTYPE_FALSE)); + acct = xs_dict_append(acct, "hide_collections", xs_stock(XSTYPE_FALSE)); + acct = xs_dict_append(acct, "indexable", xs_stock(XSTYPE_TRUE)); + acct = xs_dict_append(acct, "noindex", xs_stock(XSTYPE_FALSE)); + acct = xs_dict_append(acct, "roles", xs_stock(XSTYPE_LIST)); { /* create the acct field as user@host */ @@ -549,9 +549,9 @@ xs_dict *mastoapi_account(const xs_dict *actor) note = ""; if (strcmp(xs_dict_get(actor, "type"), "Service") == 0) - acct = xs_dict_append(acct, "bot", xs_stock_true); + acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_TRUE)); else - acct = xs_dict_append(acct, "bot", xs_stock_false); + acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_FALSE)); acct = xs_dict_append(acct, "note", note); @@ -609,7 +609,7 @@ xs_dict *mastoapi_account(const xs_dict *actor) d1 = xs_dict_append(d1, "shortcode", nm); d1 = xs_dict_append(d1, "url", url); d1 = xs_dict_append(d1, "static_url", url); - d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true); + d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE)); eml = xs_list_append(eml, d1); } @@ -620,10 +620,10 @@ xs_dict *mastoapi_account(const xs_dict *actor) acct = xs_dict_append(acct, "emojis", eml); } - acct = xs_dict_append(acct, "locked", xs_stock_false); - acct = xs_dict_append(acct, "followers_count", xs_stock_0); - acct = xs_dict_append(acct, "following_count", xs_stock_0); - acct = xs_dict_append(acct, "statuses_count", xs_stock_0); + acct = xs_dict_append(acct, "locked", xs_stock(XSTYPE_FALSE)); + acct = xs_dict_append(acct, "followers_count", xs_stock(0)); + acct = xs_dict_append(acct, "following_count", xs_stock(0)); + acct = xs_dict_append(acct, "statuses_count", xs_stock(0)); xs *fields = xs_list_new(); p = xs_dict_get(actor, "attachment"); @@ -631,19 +631,19 @@ xs_dict *mastoapi_account(const xs_dict *actor) /* dict of validated links */ xs_dict *val_links = NULL; - xs_dict *metadata = xs_stock_dict; + xs_dict *metadata = xs_stock(XSTYPE_DICT); snac user = {0}; if (xs_startswith(id, srv_baseurl)) { /* if it's a local user, open it and pick its validated links */ if (user_open(&user, prefu)) { val_links = user.links; - metadata = xs_dict_get_def(user.config, "metadata", xs_stock_dict); + metadata = xs_dict_get_def(user.config, "metadata", xs_stock(XSTYPE_DICT)); } } if (xs_is_null(val_links)) - val_links = xs_stock_dict; + val_links = xs_stock(XSTYPE_DICT); while (xs_list_iter(&p, &v)) { char *type = xs_dict_get(v, "type"); @@ -672,7 +672,7 @@ xs_dict *mastoapi_account(const xs_dict *actor) d = xs_dict_append(d, "value", value); d = xs_dict_append(d, "verified_at", xs_type(val_date) == XSTYPE_STRING && *val_date ? - val_date : xs_stock_null); + val_date : xs_stock(XSTYPE_NULL)); fields = xs_list_append(fields, d); } @@ -710,13 +710,13 @@ xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg) xs *fd = mastoapi_date(xs_dict_get(msg, "endTime")); poll = xs_dict_append(poll, "expires_at", fd); poll = xs_dict_append(poll, "expired", - xs_dict_get(msg, "closed") != NULL ? xs_stock_true : xs_stock_false); + xs_dict_get(msg, "closed") != NULL ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE)); if ((opts = xs_dict_get(msg, "oneOf")) != NULL) - poll = xs_dict_append(poll, "multiple", xs_stock_false); + poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_FALSE)); else { opts = xs_dict_get(msg, "anyOf"); - poll = xs_dict_append(poll, "multiple", xs_stock_true); + poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_TRUE)); } while (xs_list_iter(&opts, &v)) { @@ -743,7 +743,7 @@ xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg) poll = xs_dict_append(poll, "voted", (snac && was_question_voted(snac, xs_dict_get(msg, "id"))) ? - xs_stock_true : xs_stock_false); + xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE)); return poll; } @@ -809,7 +809,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) tmp = xs_dict_get(msg, "sensitive"); if (xs_is_null(tmp)) - tmp = xs_stock_false; + tmp = xs_stock(XSTYPE_FALSE); st = xs_dict_append(st, "sensitive", tmp); @@ -928,7 +928,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) d1 = xs_dict_append(d1, "shortcode", nm); d1 = xs_dict_append(d1, "url", url); d1 = xs_dict_append(d1, "static_url", url); - d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true); + d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE)); d1 = xs_dict_append(d1, "category", "Emojis"); eml = xs_list_append(eml, d1); @@ -949,7 +949,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) st = xs_dict_append(st, "favourites_count", ixc); st = xs_dict_append(st, "favourited", - (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false); + (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE)); xs_free(idx); xs_free(ixc); @@ -958,7 +958,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) st = xs_dict_append(st, "reblogs_count", ixc); st = xs_dict_append(st, "reblogged", - (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false); + (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE)); /* get the last person who boosted this */ xs *boosted_by_md5 = NULL; @@ -973,8 +973,8 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) st = xs_dict_append(st, "replies_count", ixc); /* default in_reply_to values */ - st = xs_dict_append(st, "in_reply_to_id", xs_stock_null); - st = xs_dict_append(st, "in_reply_to_account_id", xs_stock_null); + st = xs_dict_append(st, "in_reply_to_id", xs_stock(XSTYPE_NULL)); + st = xs_dict_append(st, "in_reply_to_account_id", xs_stock(XSTYPE_NULL)); tmp = xs_dict_get(msg, "inReplyTo"); if (!xs_is_null(tmp)) { @@ -992,9 +992,9 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) } } - st = xs_dict_append(st, "reblog", xs_stock_null); - st = xs_dict_append(st, "card", xs_stock_null); - st = xs_dict_append(st, "language", xs_stock_null); + st = xs_dict_append(st, "reblog", xs_stock(XSTYPE_NULL)); + st = xs_dict_append(st, "card", xs_stock(XSTYPE_NULL)); + st = xs_dict_append(st, "language", xs_stock(XSTYPE_NULL)); tmp = xs_dict_get(msg, "sourceContent"); if (xs_is_null(tmp)) @@ -1005,7 +1005,7 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) tmp = xs_dict_get(msg, "updated"); xs *fd2 = NULL; if (xs_is_null(tmp)) - tmp = xs_stock_null; + tmp = xs_stock(XSTYPE_NULL); else { fd2 = mastoapi_date(tmp); tmp = fd2; @@ -1018,12 +1018,12 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg) st = xs_dict_append(st, "poll", poll); } else - st = xs_dict_append(st, "poll", xs_stock_null); + st = xs_dict_append(st, "poll", xs_stock(XSTYPE_NULL)); - st = xs_dict_append(st, "bookmarked", xs_stock_false); + st = xs_dict_append(st, "bookmarked", xs_stock(XSTYPE_FALSE)); st = xs_dict_append(st, "pinned", - (snac && is_pinned(snac, id)) ? xs_stock_true : xs_stock_false); + (snac && is_pinned(snac, id)) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE)); /* is it a boost? */ if (!xs_is_null(boosted_by_md5)) { @@ -1067,21 +1067,21 @@ xs_dict *mastoapi_relationship(snac *snac, const char *md5) rel = xs_dict_append(rel, "id", md5); rel = xs_dict_append(rel, "following", - following_check(snac, actor) ? xs_stock_true : xs_stock_false); + following_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE)); - rel = xs_dict_append(rel, "showing_reblogs", xs_stock_true); - rel = xs_dict_append(rel, "notifying", xs_stock_false); + rel = xs_dict_append(rel, "showing_reblogs", xs_stock(XSTYPE_TRUE)); + rel = xs_dict_append(rel, "notifying", xs_stock(XSTYPE_FALSE)); rel = xs_dict_append(rel, "followed_by", - follower_check(snac, actor) ? xs_stock_true : xs_stock_false); + follower_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE)); rel = xs_dict_append(rel, "blocking", - is_muted(snac, actor) ? xs_stock_true : xs_stock_false); + is_muted(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE)); - rel = xs_dict_append(rel, "muting", xs_stock_false); - rel = xs_dict_append(rel, "muting_notifications", xs_stock_false); - rel = xs_dict_append(rel, "requested", xs_stock_false); - rel = xs_dict_append(rel, "domain_blocking", xs_stock_false); - rel = xs_dict_append(rel, "endorsed", xs_stock_false); + rel = xs_dict_append(rel, "muting", xs_stock(XSTYPE_FALSE)); + rel = xs_dict_append(rel, "muting_notifications", xs_stock(XSTYPE_FALSE)); + rel = xs_dict_append(rel, "requested", xs_stock(XSTYPE_FALSE)); + rel = xs_dict_append(rel, "domain_blocking", xs_stock(XSTYPE_FALSE)); + rel = xs_dict_append(rel, "endorsed", xs_stock(XSTYPE_FALSE)); rel = xs_dict_append(rel, "note", ""); } @@ -1149,7 +1149,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac1.config, "published")); acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio")); acct = xs_dict_append(acct, "url", snac1.actor); - acct = xs_dict_append(acct, "locked", xs_stock_false); + acct = xs_dict_append(acct, "locked", xs_stock(XSTYPE_FALSE)); acct = xs_dict_append(acct, "bot", xs_dict_get(snac1.config, "bot")); xs *src = xs_json_loads("{\"privacy\":\"public\"," @@ -1186,7 +1186,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, xs_dict *val_links = snac1.links; if (xs_is_null(val_links)) - val_links = xs_stock_dict; + val_links = xs_stock(XSTYPE_DICT); int c = 0; while (xs_dict_next(metadata, &k, &v, &c)) { @@ -1206,7 +1206,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, d = xs_dict_append(d, "value", v); d = xs_dict_append(d, "verified_at", xs_type(val_date) == XSTYPE_STRING && *val_date ? - val_date : xs_stock_null); + val_date : xs_stock(XSTYPE_NULL)); fields = xs_list_append(fields, d); } @@ -1214,9 +1214,9 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, acct = xs_dict_set(acct, "fields", fields); } - acct = xs_dict_append(acct, "followers_count", xs_stock_0); - acct = xs_dict_append(acct, "following_count", xs_stock_0); - acct = xs_dict_append(acct, "statuses_count", xs_stock_0); + acct = xs_dict_append(acct, "followers_count", xs_stock(0)); + acct = xs_dict_append(acct, "following_count", xs_stock(0)); + acct = xs_dict_append(acct, "statuses_count", xs_stock(0)); *body = xs_json_dumps(acct, 4); *ctype = "application/json"; @@ -1813,7 +1813,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, ins = xs_dict_append(ins, "email", v); - ins = xs_dict_append(ins, "rules", xs_stock_list); + ins = xs_dict_append(ins, "rules", xs_stock(XSTYPE_LIST)); xs *l1 = xs_list_append(xs_list_new(), "en"); ins = xs_dict_append(ins, "languages", l1); @@ -1824,14 +1824,14 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, ins = xs_dict_append(ins, "urls", urls); - xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock_0); - d2 = xs_dict_append(d2, "status_count", xs_stock_0); - d2 = xs_dict_append(d2, "domain_count", xs_stock_0); + xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock(0)); + d2 = xs_dict_append(d2, "status_count", xs_stock(0)); + d2 = xs_dict_append(d2, "domain_count", xs_stock(0)); ins = xs_dict_append(ins, "stats", d2); - ins = xs_dict_append(ins, "registrations", xs_stock_false); - ins = xs_dict_append(ins, "approval_required", xs_stock_false); - ins = xs_dict_append(ins, "invites_enabled", xs_stock_false); + ins = xs_dict_append(ins, "registrations", xs_stock(XSTYPE_FALSE)); + ins = xs_dict_append(ins, "approval_required", xs_stock(XSTYPE_FALSE)); + ins = xs_dict_append(ins, "invites_enabled", xs_stock(XSTYPE_FALSE)); xs *cfg = xs_dict_new(); @@ -2079,7 +2079,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, d = xs_dict_append(d, "name", q); xs *url = xs_fmt("%s?t=%s", srv_baseurl, q); d = xs_dict_append(d, "url", url); - d = xs_dict_append(d, "history", xs_stock_list); + d = xs_dict_append(d, "history", xs_stock(XSTYPE_LIST)); htl = xs_list_append(htl, d); } @@ -2257,7 +2257,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, strcmp(visibility, "public") == 0 ? 0 : 1); if (!xs_is_null(summary) && *summary) { - msg = xs_dict_set(msg, "sensitive", xs_stock_true); + msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE)); msg = xs_dict_set(msg, "summary", summary); } diff --git a/xs.h b/xs.h index 85464db..d2de44a 100644 --- a/xs.h +++ b/xs.h @@ -61,6 +61,7 @@ xs_val *xs_collapse(xs_val *data, int offset, int size); xs_val *xs_insert_m(xs_val *data, int offset, const char *mem, int size); #define xs_insert(data, offset, data2) xs_insert_m(data, offset, data2, xs_size(data2)) #define xs_append_m(data, mem, size) xs_insert_m(data, xs_size(data) - 1, mem, size) +xs_val *xs_stock(int type); xs_str *xs_str_new(const char *str); xs_str *xs_str_new_sz(const char *mem, int sz); @@ -137,34 +138,11 @@ unsigned int xs_hash_func(const char *data, int size); #define XS_ASSERT_TYPE_NULL(v, t) (void)(0) #endif -extern xs_val xs_stock_null[]; -extern xs_val xs_stock_true[]; -extern xs_val xs_stock_false[]; -extern xs_val xs_stock_0[]; -extern xs_val xs_stock_1[]; -extern xs_val xs_stock_list[]; -extern xs_val xs_stock_dict[]; - #define xs_return(v) xs_val *__r = v; v = NULL; return __r #ifdef XS_IMPLEMENTATION -xs_val xs_stock_null[] = { XSTYPE_NULL }; -xs_val xs_stock_true[] = { XSTYPE_TRUE }; -xs_val xs_stock_false[] = { XSTYPE_FALSE }; -xs_val xs_stock_0[] = { XSTYPE_NUMBER, '0', '\0' }; -xs_val xs_stock_1[] = { XSTYPE_NUMBER, '1', '\0' }; - -#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -xs_val xs_stock_list[] = { XSTYPE_LIST, 0, 0, 0, 1 + _XS_TYPE_SIZE + 1, XSTYPE_EOM }; -xs_val xs_stock_dict[] = { XSTYPE_DICT, 0, 0, 0, 1 + _XS_TYPE_SIZE + 1, XSTYPE_EOM }; -#else -xs_val xs_stock_list[] = { XSTYPE_LIST, 1 + _XS_TYPE_SIZE + 1, 0, 0, 0, XSTYPE_EOM }; -xs_val xs_stock_dict[] = { XSTYPE_DICT, 1 + _XS_TYPE_SIZE + 1, 0, 0, 0, XSTYPE_EOM }; -#endif - - void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func) { xs_val *ndata = realloc(ptr, size); @@ -369,10 +347,14 @@ int xs_cmp(const xs_val *v1, const xs_val *v2) xs_val *xs_dup(const xs_val *data) /* creates a duplicate of data */ { - int sz = xs_size(data); - xs_val *s = xs_realloc(NULL, _xs_blk_size(sz)); + xs_val *s = NULL; - memcpy(s, data, sz); + if (data) { + int sz = xs_size(data); + s = xs_realloc(NULL, _xs_blk_size(sz)); + + memcpy(s, data, sz); + } return s; } @@ -437,6 +419,39 @@ xs_val *xs_insert_m(xs_val *data, int offset, const char *mem, int size) } +xs_val *xs_stock(int type) +/* returns stock values */ +{ + static xs_val stock_null[] = { XSTYPE_NULL }; + static xs_val stock_true[] = { XSTYPE_TRUE }; + static xs_val stock_false[] = { XSTYPE_FALSE }; + static xs_val stock_0[] = { XSTYPE_NUMBER, '0', '\0' }; + static xs_val stock_1[] = { XSTYPE_NUMBER, '1', '\0' }; + static xs_list *stock_list = NULL; + static xs_dict *stock_dict = NULL; + + switch (type) { + case 0: return stock_0; + case 1: return stock_1; + case XSTYPE_NULL: return stock_null; + case XSTYPE_TRUE: return stock_true; + case XSTYPE_FALSE: return stock_false; + + case XSTYPE_LIST: + if (stock_list == NULL) + stock_list = xs_list_new(); + return stock_list; + + case XSTYPE_DICT: + if (stock_dict == NULL) + stock_dict = xs_dict_new(); + return stock_dict; + } + + return NULL; +} + + /** strings **/ xs_str *xs_str_new(const char *str) @@ -647,10 +662,14 @@ xs_str *xs_tolower_i(xs_str *str) xs_list *xs_list_new(void) /* creates a new list */ { - return memcpy( - xs_realloc(NULL, _xs_blk_size(sizeof(xs_stock_list))), - xs_stock_list, sizeof(xs_stock_list) - ); + int sz = 1 + _XS_TYPE_SIZE + 1; + xs_list *l = xs_realloc(NULL, sz); + memset(l, '\0', sz); + + l[0] = XSTYPE_LIST; + _xs_put_size(&l[1], sz); + + return l; } @@ -660,8 +679,8 @@ xs_list *_xs_list_write_litem(xs_list *list, int offset, const char *mem, int ds XS_ASSERT_TYPE(list, XSTYPE_LIST); if (mem == NULL) { - mem = xs_stock_null; - dsz = sizeof(xs_stock_null); + mem = xs_stock(XSTYPE_NULL); + dsz = xs_size(mem); } list = xs_expand(list, offset, dsz + 1); @@ -947,10 +966,14 @@ xs_list *xs_list_cat(xs_list *l1, const xs_list *l2) xs_dict *xs_dict_new(void) /* creates a new dict */ { - return memcpy( - xs_realloc(NULL, _xs_blk_size(sizeof(xs_stock_dict))), - xs_stock_dict, sizeof(xs_stock_dict) - ); + int sz = 1 + _XS_TYPE_SIZE + 1; + xs_dict *d = xs_realloc(NULL, sz); + memset(d, '\0', sz); + + d[0] = XSTYPE_DICT; + _xs_put_size(&d[1], sz); + + return d; } @@ -962,8 +985,8 @@ xs_dict *_xs_dict_write_ditem(xs_dict *dict, int offset, const xs_str *key, XS_ASSERT_TYPE(key, XSTYPE_STRING); if (data == NULL) { - data = xs_stock_null; - dsz = sizeof(xs_stock_null); + data = xs_stock(XSTYPE_NULL); + dsz = xs_size(data); } int ksz = xs_size(key); diff --git a/xs_version.h b/xs_version.h index 50dcb5e..ef52120 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* f46d5b29627b20a6e9ec4ef60c01df1d2d778520 2024-03-09T08:26:31+01:00 */ +/* 0df383371d207b0adfda40912ee54b37e5b01152 2024-03-15T03:45:39+01:00 */