Backport from xs.

This commit is contained in:
default 2023-02-16 09:32:42 +01:00
parent 75fbc38db3
commit 19ed90319b
2 changed files with 27 additions and 10 deletions

35
xs.h
View File

@ -97,6 +97,8 @@ xs_list *xs_split_n(const char *str, const char *sep, int times);
xs_dict *xs_dict_new(void); xs_dict *xs_dict_new(void);
xs_dict *xs_dict_append_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz); xs_dict *xs_dict_append_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz);
#define xs_dict_append(dict, key, data) xs_dict_append_m(dict, key, data, xs_size(data)) #define xs_dict_append(dict, key, data) xs_dict_append_m(dict, key, data, xs_size(data))
xs_dict *xs_dict_prepend_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz);
#define xs_dict_prepend(dict, key, data) xs_dict_prepend_m(dict, key, data, xs_size(data))
int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value); int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value);
xs_dict *xs_dict_get(const xs_dict *dict, const xs_str *key); xs_dict *xs_dict_get(const xs_dict *dict, const xs_str *key);
xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key); xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key);
@ -859,24 +861,39 @@ xs_dict *xs_dict_new(void)
} }
xs_dict *xs_dict_append_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz) xs_dict *xs_dict_insert_m(xs_dict *dict, int offset, const xs_str *key,
/* adds a memory block to the dict */ const xs_val *data, int dsz)
/* inserts a memory block into the dict */
{ {
XS_ASSERT_TYPE(dict, XSTYPE_DICT); XS_ASSERT_TYPE(dict, XSTYPE_DICT);
XS_ASSERT_TYPE(key, XSTYPE_STRING); XS_ASSERT_TYPE(key, XSTYPE_STRING);
char c = XSTYPE_DITEM;
int sz = xs_size(dict);
int ksz = xs_size(key); int ksz = xs_size(key);
dict = xs_insert_m(dict, sz - 1, &c, 1); dict = xs_expand(dict, offset, 1 + ksz + dsz);
dict = xs_insert_m(dict, sz, key, ksz);
dict = xs_insert_m(dict, sz + ksz, mem, dsz); dict[offset] = XSTYPE_DITEM;
memcpy(&dict[offset + 1], key, ksz);
memcpy(&dict[offset + 1 + ksz], data, dsz);
return dict; return dict;
} }
xs_dict *xs_dict_append_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz)
/* appends a memory block to the dict */
{
return xs_dict_insert_m(dict, xs_size(dict) - 1, key, mem, dsz);
}
xs_dict *xs_dict_prepend_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz)
/* prepends a memory block to the dict */
{
return xs_dict_insert_m(dict, 4, key, mem, dsz);
}
int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value) int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value)
/* iterates a dict value */ /* iterates a dict value */
{ {
@ -962,8 +979,8 @@ xs_dict *xs_dict_set(xs_dict *dict, const xs_str *key, const xs_val *data)
/* delete the possibly existing key */ /* delete the possibly existing key */
dict = xs_dict_del(dict, key); dict = xs_dict_del(dict, key);
/* append the data */ /* add the data */
dict = xs_dict_append(dict, key, data); dict = xs_dict_prepend(dict, key, data);
return dict; return dict;
} }

View File

@ -1 +1 @@
/* 79e4ff3403fbbd78f57482122a2478c9453483a8 */ /* a2221e4e87e4c2e0a92a4934f88e78ca7e8e7f7f */