Backport from xs.

This commit is contained in:
default 2023-01-28 17:49:02 +01:00
parent 5163f93c02
commit 876bebd9ac
12 changed files with 293 additions and 228 deletions

321
xs.h
View file

@ -14,25 +14,31 @@
#include <errno.h> #include <errno.h>
typedef enum { typedef enum {
XSTYPE_NULL = 0x18, XSTYPE_STRING = 0x02, /* C string (\0 delimited) (NOT STORED) */
XSTYPE_TRUE = 0x06, XSTYPE_NUMBER = 0x17, /* C string (\0 delimited) */
XSTYPE_FALSE = 0x15, XSTYPE_NULL = 0x18, /* Special NULL value */
XSTYPE_LIST = 0x11, XSTYPE_TRUE = 0x06, /* Boolean */
XSTYPE_LITEM = 0x1f, XSTYPE_FALSE = 0x15, /* Boolean */
XSTYPE_EOL = 0x12, XSTYPE_LIST = 0x1d, /* Sequence of LITEMs up to EOM (with 24bit size) */
XSTYPE_DICT = 0x13, XSTYPE_LITEM = 0x1f, /* Element of a list (any type) */
XSTYPE_DITEM = 0x1e, XSTYPE_DICT = 0x1c, /* Sequence of DITEMs up to EOM (with 24bit size) */
XSTYPE_EOD = 0x14, XSTYPE_DITEM = 0x1e, /* Element of a dict (STRING key + any type) */
XSTYPE_NUMBER = 0x17, XSTYPE_EOM = 0x19 /* End of Multiple (LIST or DICT) */
XSTYPE_STRING = 0x02
} xstype; } xstype;
/* dynamic strings */ /* dynamic strings */
typedef char d_char; typedef char d_char;
/* types */
typedef char xs_val;
typedef char xs_str;
typedef char xs_list;
typedef char xs_dict;
typedef char xs_number;
/* auto-destroyable strings */ /* auto-destroyable strings */
#define xs __attribute__ ((__cleanup__ (_xs_destroy))) d_char #define xs __attribute__ ((__cleanup__ (_xs_destroy))) xs_val
/* not really all, just very much */ /* not really all, just very much */
#define XS_ALL 0xfffffff #define XS_ALL 0xfffffff
@ -43,60 +49,75 @@ void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char
int _xs_blk_size(int sz); int _xs_blk_size(int sz);
void _xs_destroy(char **var); void _xs_destroy(char **var);
#define xs_debug() raise(SIGTRAP) #define xs_debug() raise(SIGTRAP)
xstype xs_type(const char *data); xstype xs_type(const xs_val *data);
int xs_size(const char *data); int xs_size(const xs_val *data);
int xs_is_null(const char *data); int xs_is_null(const xs_val *data);
d_char *xs_dup(const char *data); xs_val *xs_dup(const xs_val *data);
d_char *xs_expand(d_char *data, int offset, int size); xs_val *xs_expand(xs_val *data, int offset, int size);
d_char *xs_collapse(d_char *data, int offset, int size); xs_val *xs_collapse(xs_val *data, int offset, int size);
d_char *xs_insert_m(d_char *data, int offset, const char *mem, 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_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) #define xs_append_m(data, mem, size) xs_insert_m(data, xs_size(data) - 1, mem, size)
d_char *xs_str_new(const char *str);
d_char *xs_str_wrap_i(const char *prefix, d_char *str, const char *suffix); xs_str *xs_str_new(const char *str);
xs_str *xs_str_wrap_i(const char *prefix, xs_str *str, const char *suffix);
#define xs_str_prepend_i(str, prefix) xs_str_wrap_i(prefix, str, NULL) #define xs_str_prepend_i(str, prefix) xs_str_wrap_i(prefix, str, NULL)
#define xs_str_cat(str, suffix) xs_str_wrap_i(NULL, str, suffix) #define xs_str_cat(str, suffix) xs_str_wrap_i(NULL, str, suffix)
d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto); xs_str *xs_replace_i(xs_str *str, const char *sfrom, const char *sto);
#define xs_replace(str, sfrom, sto) xs_replace_i(xs_dup(str), sfrom, sto) #define xs_replace(str, sfrom, sto) xs_replace_i(xs_dup(str), sfrom, sto)
d_char *xs_fmt(const char *fmt, ...); xs_str *xs_fmt(const char *fmt, ...);
int xs_str_in(const char *haystack, const char *needle); int xs_str_in(const char *haystack, const char *needle);
int xs_startswith(const char *str, const char *prefix); int xs_startswith(const char *str, const char *prefix);
int xs_endswith(const char *str, const char *postfix); int xs_endswith(const char *str, const char *postfix);
d_char *xs_crop_i(d_char *str, int start, int end); xs_str *xs_crop_i(xs_str *str, int start, int end);
d_char *xs_strip_chars_i(d_char *str, const char *chars); xs_str *xs_strip_chars_i(xs_str *str, const char *chars);
#define xs_strip_i(str) xs_strip_chars_i(str, " \r\n\t\v\f") #define xs_strip_i(str) xs_strip_chars_i(str, " \r\n\t\v\f")
d_char *xs_tolower_i(d_char *str); xs_str *xs_tolower_i(xs_str *str);
d_char *xs_list_new(void);
d_char *xs_list_append_m(d_char *list, const char *mem, int dsz); xs_list *xs_list_new(void);
xs_list *xs_list_append_m(xs_list *list, const char *mem, int dsz);
#define xs_list_append(list, data) xs_list_append_m(list, data, xs_size(data)) #define xs_list_append(list, data) xs_list_append_m(list, data, xs_size(data))
int xs_list_iter(char **list, char **value); int xs_list_iter(xs_list **list, xs_val **value);
int xs_list_len(char *list); int xs_list_len(xs_list *list);
char *xs_list_get(char *list, int num); char *xs_list_get(xs_list *list, int num);
d_char *xs_list_del(d_char *list, int num); xs_list *xs_list_del(xs_list *list, int num);
d_char *xs_list_insert(d_char *list, int num, const char *data); xs_list *xs_list_insert(xs_list *list, int num, const xs_val *data);
d_char *xs_list_insert_sorted(d_char *list, const char *str); xs_list *xs_list_insert_sorted(xs_list *list, const char *str);
d_char *xs_list_set(d_char *list, int num, const char *data); xs_list *xs_list_set(xs_list *list, int num, const xs_val *data);
d_char *xs_list_dequeue(d_char *list, char **data, int last); xs_list *xs_list_dequeue(xs_list *list, xs_val **data, int last);
#define xs_list_pop(list, data) xs_list_dequeue(list, data, 1) #define xs_list_pop(list, data) xs_list_dequeue(list, data, 1)
#define xs_list_shift(list, data) xs_list_dequeue(list, data, 0) #define xs_list_shift(list, data) xs_list_dequeue(list, data, 0)
int xs_list_in(char *list, const char *val); int xs_list_in(xs_list *list, const xs_val *val);
d_char *xs_join(char *list, const char *sep); xs_str *xs_join(xs_list *list, const char *sep);
d_char *xs_split_n(const char *str, const char *sep, int times); xs_list *xs_split_n(const char *str, const char *sep, int times);
#define xs_split(str, sep) xs_split_n(str, sep, XS_ALL) #define xs_split(str, sep) xs_split_n(str, sep, XS_ALL)
d_char *xs_dict_new(void);
d_char *xs_dict_append_m(d_char *dict, const char *key, const char *mem, int dsz); 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);
#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))
int xs_dict_iter(char **dict, char **key, char **value); int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value);
char *xs_dict_get(char *dict, const char *key); xs_dict *xs_dict_get(xs_dict *dict, const xs_str *key);
d_char *xs_dict_del(d_char *dict, const char *key); xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key);
d_char *xs_dict_set(d_char *dict, const char *key, const char *data); xs_dict *xs_dict_set(xs_dict *dict, const xs_str *key, const xs_val *data);
d_char *xs_val_new(xstype t);
d_char *xs_number_new(double f); xs_val *xs_val_new(xstype t);
double xs_number_get(const char *v); xs_number *xs_number_new(double f);
const char *xs_number_str(const char *v); double xs_number_get(const xs_number *v);
const char *xs_number_str(const xs_number *v);
void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size);
#ifdef XS_ASSERT
#include <assert.h>
#define XS_ASSERT_TYPE(v, t) assert(xs_type(v) == t)
#define XS_ASSERT_TYPE_NULL(v, t) assert(v == NULL || xs_type(v) == t)
#else
#define XS_ASSERT_TYPE(v, t) (void)(0)
#define XS_ASSERT_TYPE_NULL(v, t) (void)(0)
#endif
#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)
@ -183,22 +204,24 @@ int _xs_blk_size(int sz)
} }
xstype xs_type(const char *data) xstype xs_type(const xs_val *data)
/* return the type of data */ /* return the type of data */
{ {
xstype t; xstype t;
if (data == NULL)
t = XSTYPE_NULL;
else
switch (data[0]) { switch (data[0]) {
case XSTYPE_NULL: case XSTYPE_NULL:
case XSTYPE_TRUE: case XSTYPE_TRUE:
case XSTYPE_FALSE: case XSTYPE_FALSE:
case XSTYPE_LIST: case XSTYPE_LIST:
case XSTYPE_EOL:
case XSTYPE_DICT:
case XSTYPE_EOD:
case XSTYPE_LITEM: case XSTYPE_LITEM:
case XSTYPE_DICT:
case XSTYPE_DITEM: case XSTYPE_DITEM:
case XSTYPE_NUMBER: case XSTYPE_NUMBER:
case XSTYPE_EOM:
t = data[0]; t = data[0];
break; break;
default: default:
@ -210,7 +233,7 @@ xstype xs_type(const char *data)
} }
void _xs_put_24b(char *ptr, int i) void _xs_put_24b(xs_val *ptr, int i)
/* writes i as a 24 bit value */ /* writes i as a 24 bit value */
{ {
unsigned char *p = (unsigned char *)ptr; unsigned char *p = (unsigned char *)ptr;
@ -221,7 +244,7 @@ void _xs_put_24b(char *ptr, int i)
} }
int _xs_get_24b(const char *ptr) int _xs_get_24b(const xs_val *ptr)
/* reads a 24 bit value */ /* reads a 24 bit value */
{ {
unsigned char *p = (unsigned char *)ptr; unsigned char *p = (unsigned char *)ptr;
@ -230,7 +253,7 @@ int _xs_get_24b(const char *ptr)
} }
int xs_size(const char *data) int xs_size(const xs_val *data)
/* returns the size of data in bytes */ /* returns the size of data in bytes */
{ {
int len = 0; int len = 0;
@ -286,18 +309,18 @@ int xs_size(const char *data)
} }
int xs_is_null(const char *data) int xs_is_null(const xs_val *data)
/* checks for null */ /* checks for null */
{ {
return !!(data == NULL || xs_type(data) == XSTYPE_NULL); return (xs_type(data) == XSTYPE_NULL);
} }
d_char *xs_dup(const char *data) xs_val *xs_dup(const xs_val *data)
/* creates a duplicate of data */ /* creates a duplicate of data */
{ {
int sz = xs_size(data); int sz = xs_size(data);
d_char *s = xs_realloc(NULL, _xs_blk_size(sz)); xs_val *s = xs_realloc(NULL, _xs_blk_size(sz));
memcpy(s, data, sz); memcpy(s, data, sz);
@ -305,7 +328,7 @@ d_char *xs_dup(const char *data)
} }
d_char *xs_expand(d_char *data, int offset, int size) xs_val *xs_expand(xs_val *data, int offset, int size)
/* opens a hole in data */ /* opens a hole in data */
{ {
int sz = xs_size(data); int sz = xs_size(data);
@ -325,7 +348,7 @@ d_char *xs_expand(d_char *data, int offset, int size)
} }
d_char *xs_collapse(d_char *data, int offset, int size) xs_val *xs_collapse(xs_val *data, int offset, int size)
/* shrinks data */ /* shrinks data */
{ {
int sz = xs_size(data); int sz = xs_size(data);
@ -348,7 +371,7 @@ d_char *xs_collapse(d_char *data, int offset, int size)
} }
d_char *xs_insert_m(d_char *data, int offset, const char *mem, int size) xs_val *xs_insert_m(xs_val *data, int offset, const char *mem, int size)
/* inserts a memory block */ /* inserts a memory block */
{ {
data = xs_expand(data, offset, size); data = xs_expand(data, offset, size);
@ -360,16 +383,18 @@ d_char *xs_insert_m(d_char *data, int offset, const char *mem, int size)
/** strings **/ /** strings **/
d_char *xs_str_new(const char *str) xs_str *xs_str_new(const char *str)
/* creates a new string */ /* creates a new string */
{ {
return xs_insert(NULL, 0, str ? str : ""); return xs_insert(NULL, 0, str ? str : "");
} }
d_char *xs_str_wrap_i(const char *prefix, d_char *str, const char *suffix) xs_str *xs_str_wrap_i(const char *prefix, xs_str *str, const char *suffix)
/* wraps str with prefix and suffix */ /* wraps str with prefix and suffix */
{ {
XS_ASSERT_TYPE(str, XSTYPE_STRING);
if (prefix) if (prefix)
str = xs_insert_m(str, 0, prefix, strlen(prefix)); str = xs_insert_m(str, 0, prefix, strlen(prefix));
@ -380,9 +405,11 @@ d_char *xs_str_wrap_i(const char *prefix, d_char *str, const char *suffix)
} }
d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto) xs_str *xs_replace_i(xs_str *str, const char *sfrom, const char *sto)
/* replaces inline all sfrom with sto */ /* replaces inline all sfrom with sto */
{ {
XS_ASSERT_TYPE(str, XSTYPE_STRING);
int sfsz = strlen(sfrom); int sfsz = strlen(sfrom);
int stsz = strlen(sto); int stsz = strlen(sto);
char *ss; char *ss;
@ -402,11 +429,11 @@ d_char *xs_replace_i(d_char *str, const char *sfrom, const char *sto)
} }
d_char *xs_fmt(const char *fmt, ...) xs_str *xs_fmt(const char *fmt, ...)
/* formats a string with printf()-like marks */ /* formats a string with printf()-like marks */
{ {
int n; int n;
d_char *s = NULL; xs_str *s = NULL;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
@ -455,9 +482,11 @@ int xs_endswith(const char *str, const char *postfix)
} }
d_char *xs_crop_i(d_char *str, int start, int end) xs_str *xs_crop_i(xs_str *str, int start, int end)
/* crops the d_char to be only from start to end */ /* crops the d_char to be only from start to end */
{ {
XS_ASSERT_TYPE(str, XSTYPE_STRING);
int sz = strlen(str); int sz = strlen(str);
if (end <= 0) if (end <= 0)
@ -473,9 +502,11 @@ d_char *xs_crop_i(d_char *str, int start, int end)
} }
d_char *xs_strip_chars_i(d_char *str, const char *chars) xs_str *xs_strip_chars_i(xs_str *str, const char *chars)
/* strips the string of chars from the start and the end */ /* strips the string of chars from the start and the end */
{ {
XS_ASSERT_TYPE(str, XSTYPE_STRING);
int n; int n;
/* strip first from the end */ /* strip first from the end */
@ -494,9 +525,11 @@ d_char *xs_strip_chars_i(d_char *str, const char *chars)
} }
d_char *xs_tolower_i(d_char *str) xs_str *xs_tolower_i(xs_str *str)
/* convert to lowercase */ /* convert to lowercase */
{ {
XS_ASSERT_TYPE(str, XSTYPE_STRING);
int n; int n;
for (n = 0; str[n]; n++) for (n = 0; str[n]; n++)
@ -508,14 +541,14 @@ d_char *xs_tolower_i(d_char *str)
/** lists **/ /** lists **/
d_char *xs_list_new(void) xs_list *xs_list_new(void)
/* creates a new list */ /* creates a new list */
{ {
d_char *list; xs_list *list;
list = xs_realloc(NULL, _xs_blk_size(5)); list = xs_realloc(NULL, _xs_blk_size(5));
list[0] = XSTYPE_LIST; list[0] = XSTYPE_LIST;
list[4] = XSTYPE_EOL; list[4] = XSTYPE_EOM;
_xs_put_24b(list + 1, 5); _xs_put_24b(list + 1, 5);
@ -523,9 +556,11 @@ d_char *xs_list_new(void)
} }
d_char *_xs_list_write_litem(d_char *list, int offset, const char *mem, int dsz) xs_list *_xs_list_write_litem(xs_list *list, int offset, const char *mem, int dsz)
/* writes a list item */ /* writes a list item */
{ {
XS_ASSERT_TYPE(list, XSTYPE_LIST);
char c = XSTYPE_LITEM; char c = XSTYPE_LITEM;
list = xs_insert_m(list, offset, &c, 1); list = xs_insert_m(list, offset, &c, 1);
@ -535,30 +570,28 @@ d_char *_xs_list_write_litem(d_char *list, int offset, const char *mem, int dsz)
} }
d_char *xs_list_append_m(d_char *list, const char *mem, int dsz) xs_list *xs_list_append_m(xs_list *list, const char *mem, int dsz)
/* adds a memory block to the list */ /* adds a memory block to the list */
{ {
XS_ASSERT_TYPE(list, XSTYPE_LIST);
return _xs_list_write_litem(list, xs_size(list) - 1, mem, dsz); return _xs_list_write_litem(list, xs_size(list) - 1, mem, dsz);
} }
int xs_list_iter(char **list, char **value) int xs_list_iter(xs_list **list, xs_val **value)
/* iterates a list value */ /* iterates a list value */
{ {
int goon = 1; int goon = 1;
char *p;
if (list == NULL || *list == NULL) xs_val *p = *list;
return 0;
p = *list;
/* skip the start of the list */ /* skip the start of the list */
if (*p == XSTYPE_LIST) if (xs_type(p) == XSTYPE_LIST)
p += 4; p += 4;
/* an element? */ /* an element? */
if (*p == XSTYPE_LITEM) { if (xs_type(p) == XSTYPE_LITEM) {
p++; p++;
*value = p; *value = p;
@ -567,7 +600,6 @@ int xs_list_iter(char **list, char **value)
} }
else { else {
/* end of list */ /* end of list */
p++;
goon = 0; goon = 0;
} }
@ -578,11 +610,13 @@ int xs_list_iter(char **list, char **value)
} }
int xs_list_len(char *list) int xs_list_len(xs_list *list)
/* returns the number of elements in the list */ /* returns the number of elements in the list */
{ {
XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
int c = 0; int c = 0;
char *v; xs_val *v;
while (xs_list_iter(&list, &v)) while (xs_list_iter(&list, &v))
c++; c++;
@ -591,11 +625,13 @@ int xs_list_len(char *list)
} }
char *xs_list_get(char *list, int num) xs_val *xs_list_get(xs_list *list, int num)
/* returns the element #num */ /* returns the element #num */
{ {
char *v; XS_ASSERT_TYPE(list, XSTYPE_LIST);
int c = 0; int c = 0;
xs_val *v;
if (num < 0) if (num < 0)
num = xs_list_len(list) + num; num = xs_list_len(list) + num;
@ -611,10 +647,12 @@ char *xs_list_get(char *list, int num)
} }
d_char *xs_list_del(d_char *list, int num) xs_list *xs_list_del(xs_list *list, int num)
/* deletes element #num */ /* deletes element #num */
{ {
char *v; XS_ASSERT_TYPE(list, XSTYPE_LIST);
xs_val *v;
if ((v = xs_list_get(list, num)) != NULL) if ((v = xs_list_get(list, num)) != NULL)
list = xs_collapse(list, v - 1 - list, xs_size(v - 1)); list = xs_collapse(list, v - 1 - list, xs_size(v - 1));
@ -623,10 +661,12 @@ d_char *xs_list_del(d_char *list, int num)
} }
d_char *xs_list_insert(d_char *list, int num, const char *data) xs_list *xs_list_insert(xs_list *list, int num, const xs_val *data)
/* inserts an element at #num position */ /* inserts an element at #num position */
{ {
char *v; XS_ASSERT_TYPE(list, XSTYPE_LIST);
xs_val *v;
int offset; int offset;
if ((v = xs_list_get(list, num)) != NULL) if ((v = xs_list_get(list, num)) != NULL)
@ -638,9 +678,12 @@ d_char *xs_list_insert(d_char *list, int num, const char *data)
} }
d_char *xs_list_insert_sorted(d_char *list, const char *str) xs_list *xs_list_insert_sorted(xs_list *list, const xs_str *str)
/* inserts a string in the list in its ordered position */ /* inserts a string in the list in its ordered position */
{ {
XS_ASSERT_TYPE(list, XSTYPE_LIST);
XS_ASSERT_TYPE(str, XSTYPE_STRING);
char *p, *v; char *p, *v;
int offset = xs_size(list); int offset = xs_size(list);
@ -657,9 +700,11 @@ d_char *xs_list_insert_sorted(d_char *list, const char *str)
} }
d_char *xs_list_set(d_char *list, int num, const char *data) xs_list *xs_list_set(xs_list *list, int num, const xs_val *data)
/* sets the element at #num position */ /* sets the element at #num position */
{ {
XS_ASSERT_TYPE(list, XSTYPE_LIST);
list = xs_list_del(list, num); list = xs_list_del(list, num);
list = xs_list_insert(list, num, data); list = xs_list_insert(list, num, data);
@ -667,10 +712,13 @@ d_char *xs_list_set(d_char *list, int num, const char *data)
} }
d_char *xs_list_dequeue(d_char *list, char **data, int last) xs_list *xs_list_dequeue(xs_list *list, xs_val **data, int last)
/* gets a copy of the first or last element of a list, shrinking it */ /* gets a copy of the first or last element of a list, shrinking it */
{ {
char *p = list, *v = NULL; XS_ASSERT_TYPE(list, XSTYPE_LIST);
xs_list *p = list;
xs_val *v = NULL;
if (!last) { if (!last) {
/* get the first */ /* get the first */
@ -692,11 +740,13 @@ d_char *xs_list_dequeue(d_char *list, char **data, int last)
} }
int xs_list_in(char *list, const char *val) int xs_list_in(xs_list *list, const xs_val *val)
/* returns the position of val in list or -1 */ /* returns the position of val in list or -1 */
{ {
XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
int n = 0; int n = 0;
char *v; xs_val *v;
int sz = xs_size(val); int sz = xs_size(val);
while (xs_list_iter(&list, &v)) { while (xs_list_iter(&list, &v)) {
@ -710,11 +760,13 @@ int xs_list_in(char *list, const char *val)
} }
d_char *xs_join(char *list, const char *sep) xs_str *xs_join(xs_list *list, const char *sep)
/* joins a list into a string */ /* joins a list into a string */
{ {
d_char *s = NULL; XS_ASSERT_TYPE(list, XSTYPE_LIST);
char *v;
xs_str *s = NULL;
xs_val *v;
int c = 0; int c = 0;
int offset = 0; int offset = 0;
int ssz = strlen(sep); int ssz = strlen(sep);
@ -749,12 +801,12 @@ d_char *xs_join(char *list, const char *sep)
} }
d_char *xs_split_n(const char *str, const char *sep, int times) xs_list *xs_split_n(const char *str, const char *sep, int times)
/* splits a string into a list upto n times */ /* splits a string into a list upto n times */
{ {
int sz = strlen(sep); int sz = strlen(sep);
char *ss; char *ss;
d_char *list; xs_list *list;
list = xs_list_new(); list = xs_list_new();
@ -763,7 +815,7 @@ d_char *xs_split_n(const char *str, const char *sep, int times)
list = xs_list_append_m(list, str, ss - str); list = xs_list_append_m(list, str, ss - str);
/* add the asciiz */ /* add the asciiz */
list = xs_str_cat(list, ""); list = xs_insert_m(list, xs_size(list) - 1, "", 1);
/* skip past the separator */ /* skip past the separator */
str = ss + sz; str = ss + sz;
@ -780,14 +832,14 @@ d_char *xs_split_n(const char *str, const char *sep, int times)
/** dicts **/ /** dicts **/
d_char *xs_dict_new(void) xs_dict *xs_dict_new(void)
/* creates a new dict */ /* creates a new dict */
{ {
d_char *dict; xs_dict *dict;
dict = xs_realloc(NULL, _xs_blk_size(5)); dict = xs_realloc(NULL, _xs_blk_size(5));
dict[0] = XSTYPE_DICT; dict[0] = XSTYPE_DICT;
dict[4] = XSTYPE_EOD; dict[4] = XSTYPE_EOM;
_xs_put_24b(dict + 1, 5); _xs_put_24b(dict + 1, 5);
@ -795,9 +847,12 @@ d_char *xs_dict_new(void)
} }
d_char *xs_dict_append_m(d_char *dict, const char *key, const char *mem, int dsz) xs_dict *xs_dict_append_m(xs_dict *dict, const xs_str *key, const xs_val *mem, int dsz)
/* adds a memory block to the dict */ /* adds a memory block to the dict */
{ {
XS_ASSERT_TYPE(dict, XSTYPE_DICT);
XS_ASSERT_TYPE(key, XSTYPE_STRING);
char c = XSTYPE_DITEM; char c = XSTYPE_DITEM;
int sz = xs_size(dict); int sz = xs_size(dict);
int ksz = xs_size(key); int ksz = xs_size(key);
@ -810,23 +865,19 @@ d_char *xs_dict_append_m(d_char *dict, const char *key, const char *mem, int dsz
} }
int xs_dict_iter(char **dict, char **key, char **value) int xs_dict_iter(xs_dict **dict, xs_str **key, xs_val **value)
/* iterates a dict value */ /* iterates a dict value */
{ {
int goon = 1; int goon = 1;
char *p;
if (dict == NULL || *dict == NULL) xs_val *p = *dict;
return 0;
p = *dict;
/* skip the start of the list */ /* skip the start of the list */
if (*p == XSTYPE_DICT) if (xs_type(p) == XSTYPE_DICT)
p += 4; p += 4;
/* an element? */ /* an element? */
if (*p == XSTYPE_DITEM) { if (xs_type(p) == XSTYPE_DITEM) {
p++; p++;
*key = p; *key = p;
@ -837,7 +888,6 @@ int xs_dict_iter(char **dict, char **key, char **value)
} }
else { else {
/* end of list */ /* end of list */
p++;
goon = 0; goon = 0;
} }
@ -848,10 +898,14 @@ int xs_dict_iter(char **dict, char **key, char **value)
} }
char *xs_dict_get(char *dict, const char *key) xs_val *xs_dict_get(xs_dict *dict, const xs_str *key)
/* returns the value directed by key */ /* returns the value directed by key */
{ {
char *k, *v; XS_ASSERT_TYPE(dict, XSTYPE_DICT);
XS_ASSERT_TYPE(key, XSTYPE_STRING);
xs_str *k;
xs_val *v;
while (xs_dict_iter(&dict, &k, &v)) { while (xs_dict_iter(&dict, &k, &v)) {
if (strcmp(k, key) == 0) if (strcmp(k, key) == 0)
@ -862,11 +916,15 @@ char *xs_dict_get(char *dict, const char *key)
} }
d_char *xs_dict_del(d_char *dict, const char *key) xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key)
/* deletes a key */ /* deletes a key */
{ {
char *k, *v; XS_ASSERT_TYPE(dict, XSTYPE_DICT);
char *p = dict; XS_ASSERT_TYPE(key, XSTYPE_STRING);
xs_str *k;
xs_val *v;
xs_dict *p = dict;
while (xs_dict_iter(&p, &k, &v)) { while (xs_dict_iter(&p, &k, &v)) {
if (strcmp(k, key) == 0) { if (strcmp(k, key) == 0) {
@ -882,9 +940,12 @@ d_char *xs_dict_del(d_char *dict, const char *key)
} }
d_char *xs_dict_set(d_char *dict, const char *key, const char *data) xs_dict *xs_dict_set(xs_dict *dict, const xs_str *key, const xs_val *data)
/* sets (replaces) a key */ /* sets (replaces) a key */
{ {
XS_ASSERT_TYPE(dict, XSTYPE_DICT);
XS_ASSERT_TYPE(key, XSTYPE_STRING);
/* delete the possibly existing key */ /* delete the possibly existing key */
dict = xs_dict_del(dict, key); dict = xs_dict_del(dict, key);
@ -897,10 +958,10 @@ d_char *xs_dict_set(d_char *dict, const char *key, const char *data)
/** other values **/ /** other values **/
d_char *xs_val_new(xstype t) xs_val *xs_val_new(xstype t)
/* adds a new special value */ /* adds a new special value */
{ {
d_char *v = xs_realloc(NULL, _xs_blk_size(1)); xs_val *v = xs_realloc(NULL, _xs_blk_size(1));
v[0] = t; v[0] = t;
@ -910,10 +971,10 @@ d_char *xs_val_new(xstype t)
/** numbers */ /** numbers */
d_char *xs_number_new(double f) xs_number *xs_number_new(double f)
/* adds a new number value */ /* adds a new number value */
{ {
d_char *v; xs_number *v;
char tmp[64]; char tmp[64];
snprintf(tmp, sizeof(tmp), "%.15lf", f); snprintf(tmp, sizeof(tmp), "%.15lf", f);
@ -940,7 +1001,7 @@ d_char *xs_number_new(double f)
} }
double xs_number_get(const char *v) double xs_number_get(const xs_number *v)
/* gets the number as a double */ /* gets the number as a double */
{ {
double f = 0.0; double f = 0.0;
@ -952,7 +1013,7 @@ double xs_number_get(const char *v)
} }
const char *xs_number_str(const char *v) const char *xs_number_str(const xs_number *v)
/* gets the number as a string */ /* gets the number as a string */
{ {
const char *p = NULL; const char *p = NULL;

View file

@ -4,18 +4,18 @@
#define _XS_CURL_H #define _XS_CURL_H
d_char *xs_http_request(char *method, char *url, d_char *headers, xs_dict *xs_http_request(char *method, char *url, xs_dict *headers,
d_char *body, int b_size, int *status, xs_str *body, int b_size, int *status,
d_char **payload, int *p_size, int timeout); xs_str **payload, int *p_size, int timeout);
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
#include <curl/curl.h> #include <curl/curl.h>
static size_t _header_callback(char *buffer, size_t size, static size_t _header_callback(char *buffer, size_t size,
size_t nitems, d_char **userdata) size_t nitems, xs_dict **userdata)
{ {
d_char *headers = *userdata; xs_dict *headers = *userdata;
xs *l; xs *l;
/* get the line */ /* get the line */
@ -83,15 +83,17 @@ static int _post_callback(char *buffer, size_t size,
} }
d_char *xs_http_request(char *method, char *url, d_char *headers, xs_dict *xs_http_request(char *method, char *url, xs_dict *headers,
d_char *body, int b_size, int *status, xs_str *body, int b_size, int *status,
d_char **payload, int *p_size, int timeout) xs_str **payload, int *p_size, int timeout)
/* does an HTTP request */ /* does an HTTP request */
{ {
d_char *response; xs_dict *response;
CURL *curl; CURL *curl;
struct curl_slist *list = NULL; struct curl_slist *list = NULL;
char *k, *v, *p; xs_dict *p;
xs_str *k;
xs_val *v;
long lstatus; long lstatus;
struct _payload_data pd; struct _payload_data pd;

View file

@ -4,19 +4,19 @@
#define _XS_ENCDEC_H #define _XS_ENCDEC_H
d_char *xs_hex_enc(const char *data, int size); xs_str *xs_hex_enc(const xs_val *data, int size);
d_char *xs_hex_dec(const char *hex, int *size); xs_val *xs_hex_dec(const xs_str *hex, int *size);
d_char *xs_base64_enc(const char *data, int sz); xs_str *xs_base64_enc(const xs_val *data, int sz);
d_char *xs_base64_dec(const char *data, int *size); xs_val *xs_base64_dec(const xs_str *data, int *size);
d_char *xs_utf8_enc(d_char *str, unsigned int cpoint); xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint);
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
d_char *xs_hex_enc(const char *data, int size) xs_str *xs_hex_enc(const xs_val *data, int size)
/* returns an hexdump of data */ /* returns an hexdump of data */
{ {
d_char *s; xs_str *s;
char *p; char *p;
int n; int n;
@ -33,11 +33,11 @@ d_char *xs_hex_enc(const char *data, int size)
} }
d_char *xs_hex_dec(const char *hex, int *size) xs_val *xs_hex_dec(const xs_str *hex, int *size)
/* decodes an hexdump into data */ /* decodes an hexdump into data */
{ {
int sz = strlen(hex); int sz = strlen(hex);
d_char *s = NULL; xs_val *s = NULL;
char *p; char *p;
int n; int n;
@ -65,10 +65,10 @@ d_char *xs_hex_dec(const char *hex, int *size)
} }
d_char *xs_base64_enc(const char *data, int sz) xs_str *xs_base64_enc(const xs_val *data, int sz)
/* encodes data to base64 */ /* encodes data to base64 */
{ {
d_char *s; xs_str *s;
unsigned char *p; unsigned char *p;
char *i; char *i;
int bsz, n; int bsz, n;
@ -110,10 +110,10 @@ d_char *xs_base64_enc(const char *data, int sz)
} }
d_char *xs_base64_dec(const char *data, int *size) xs_val *xs_base64_dec(const xs_str *data, int *size)
/* decodes data from base64 */ /* decodes data from base64 */
{ {
d_char *s = NULL; xs_val *s = NULL;
int sz = 0; int sz = 0;
char *p; char *p;
static char *b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" static char *b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@ -171,7 +171,7 @@ d_char *xs_base64_dec(const char *data, int *size)
} }
d_char *xs_utf8_enc(d_char *str, unsigned int cpoint) xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint)
/* encodes an Unicode codepoint to utf8 */ /* encodes an Unicode codepoint to utf8 */
{ {
unsigned char tmp[4]; unsigned char tmp[4];

View file

@ -4,7 +4,7 @@
#define _XS_GLOB_H #define _XS_GLOB_H
d_char *xs_glob_n(const char *spec, int basename, int reverse, int max); xs_list *xs_glob_n(const char *spec, int basename, int reverse, int max);
#define xs_glob(spec, basename, reverse) xs_glob_n(spec, basename, reverse, XS_ALL) #define xs_glob(spec, basename, reverse) xs_glob_n(spec, basename, reverse, XS_ALL)
@ -12,11 +12,11 @@ d_char *xs_glob_n(const char *spec, int basename, int reverse, int max);
#include <glob.h> #include <glob.h>
d_char *xs_glob_n(const char *spec, int basename, int reverse, int max) xs_list *xs_glob_n(const char *spec, int basename, int reverse, int max)
/* does a globbing and returns the found files */ /* does a globbing and returns the found files */
{ {
glob_t globbuf; glob_t globbuf;
d_char *list = xs_list_new(); xs_list *list = xs_list_new();
if (glob(spec, 0, NULL, &globbuf) == 0) { if (glob(spec, 0, NULL, &globbuf) == 0) {
int n; int n;

View file

@ -4,18 +4,18 @@
#define _XS_HTTPD_H #define _XS_HTTPD_H
d_char *xs_url_dec(char *str); xs_str *xs_url_dec(char *str);
d_char *xs_url_vars(char *str); xs_dict *xs_url_vars(char *str);
d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size); xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size);
void xs_httpd_response(FILE *f, int status, d_char *headers, char *body, int b_size); void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size);
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
d_char *xs_url_dec(char *str) xs_str *xs_url_dec(char *str)
/* decodes an URL */ /* decodes an URL */
{ {
d_char *s = xs_str_new(NULL); xs_str *s = xs_str_new(NULL);
while (*str) { while (*str) {
if (*str == '%') { if (*str == '%') {
@ -41,19 +41,19 @@ d_char *xs_url_dec(char *str)
} }
d_char *xs_url_vars(char *str) xs_dict *xs_url_vars(char *str)
/* parse url variables */ /* parse url variables */
{ {
d_char *vars; xs_dict *vars;
vars = xs_dict_new(); vars = xs_dict_new();
if (str != NULL) { if (str != NULL) {
char *v, *l;
xs *args;
/* split by arguments */ /* split by arguments */
args = xs_split(str, "&"); xs *args = xs_split(str, "&");
xs_list *l;
xs_val *v;
l = args; l = args;
while (xs_list_iter(&l, &v)) { while (xs_list_iter(&l, &v)) {
@ -69,7 +69,7 @@ d_char *xs_url_vars(char *str)
} }
d_char *_xs_multipart_form_data(char *payload, int p_size, char *header) xs_dict *_xs_multipart_form_data(xs_str *payload, int p_size, char *header)
/* parses a multipart/form-data payload */ /* parses a multipart/form-data payload */
{ {
xs *boundary = NULL; xs *boundary = NULL;
@ -89,7 +89,7 @@ d_char *_xs_multipart_form_data(char *payload, int p_size, char *header)
bsz = strlen(boundary); bsz = strlen(boundary);
d_char *p_vars = xs_dict_new(); xs_dict *p_vars = xs_dict_new();
/* iterate searching the boundaries */ /* iterate searching the boundaries */
while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) { while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) {
@ -173,10 +173,9 @@ d_char *_xs_multipart_form_data(char *payload, int p_size, char *header)
} }
d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size)
/* processes an httpd connection */ /* processes an httpd connection */
{ {
d_char *req = NULL;
xs *q_vars = NULL; xs *q_vars = NULL;
xs *p_vars = NULL; xs *p_vars = NULL;
xs *l1, *l2; xs *l1, *l2;
@ -193,7 +192,7 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
return NULL; return NULL;
} }
req = xs_dict_new(); xs_dict *req = xs_dict_new();
req = xs_dict_append(req, "method", xs_list_get(l2, 0)); req = xs_dict_append(req, "method", xs_list_get(l2, 0));
req = xs_dict_append(req, "proto", xs_list_get(l2, 2)); req = xs_dict_append(req, "proto", xs_list_get(l2, 2));
@ -258,11 +257,13 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
} }
void xs_httpd_response(FILE *f, int status, d_char *headers, char *body, int b_size) void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size)
/* sends an httpd response */ /* sends an httpd response */
{ {
xs *proto; xs *proto;
char *p, *k, *v; xs_dict *p;
xs_str *k;
xs_val *v;
proto = xs_fmt("HTTP/1.1 %d ", status); proto = xs_fmt("HTTP/1.1 %d ", status);
fprintf(f, "%s\r\n", proto); fprintf(f, "%s\r\n", proto);

16
xs_io.h
View file

@ -4,17 +4,17 @@
#define _XS_IO_H #define _XS_IO_H
d_char *xs_readline(FILE *f); xs_str *xs_readline(FILE *f);
d_char *xs_read(FILE *f, int *size); xs_val *xs_read(FILE *f, int *size);
d_char *xs_readall(FILE *f); xs_val *xs_readall(FILE *f);
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
d_char *xs_readline(FILE *f) xs_str *xs_readline(FILE *f)
/* reads a line from a file */ /* reads a line from a file */
{ {
d_char *s = NULL; xs_str *s = NULL;
errno = 0; errno = 0;
@ -38,10 +38,10 @@ d_char *xs_readline(FILE *f)
} }
d_char *xs_read(FILE *f, int *sz) xs_val *xs_read(FILE *f, int *sz)
/* reads up to size bytes from f */ /* reads up to size bytes from f */
{ {
d_char *s = NULL; xs_val *s = NULL;
int size = *sz; int size = *sz;
int rdsz = 0; int rdsz = 0;
@ -76,7 +76,7 @@ d_char *xs_read(FILE *f, int *sz)
} }
d_char *xs_readall(FILE *f) xs_val *xs_readall(FILE *f)
/* reads the rest of the file into a string */ /* reads the rest of the file into a string */
{ {
int size = XS_ALL; int size = XS_ALL;

View file

@ -4,9 +4,9 @@
#define _XS_JSON_H #define _XS_JSON_H
d_char *xs_json_dumps_pp(char *data, int indent); xs_str *xs_json_dumps_pp(xs_val *data, int indent);
#define xs_json_dumps(data) xs_json_dumps_pp(data, 0) #define xs_json_dumps(data) xs_json_dumps_pp(data, 0)
d_char *xs_json_loads(const char *json); xs_val *xs_json_loads(const xs_str *json);
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
@ -148,11 +148,11 @@ d_char *_xs_json_dumps(d_char *s, char *data, int level, int indent)
} }
d_char *xs_json_dumps_pp(char *data, int indent) xs_str *xs_json_dumps_pp(xs_val *data, int indent)
/* dumps a piece of data as JSON */ /* dumps a piece of data as JSON */
{ {
xstype t = xs_type(data); xstype t = xs_type(data);
d_char *s = NULL; xs_str *s = NULL;
if (t == XSTYPE_LIST || t == XSTYPE_DICT) { if (t == XSTYPE_LIST || t == XSTYPE_DICT) {
s = xs_str_new(NULL); s = xs_str_new(NULL);
@ -481,10 +481,10 @@ d_char *_xs_json_loads_object(const char **json, js_type *t)
} }
d_char *xs_json_loads(const char *json) xs_val *xs_json_loads(const xs_str *json)
/* loads a string in JSON format and converts to a multiple data */ /* loads a string in JSON format and converts to a multiple data */
{ {
d_char *v = NULL; xs_val *v = NULL;
js_type t; js_type t;
xs_free(_xs_json_loads_lexer(&json, &t)); xs_free(_xs_json_loads_lexer(&json, &t));

View file

@ -4,14 +4,14 @@
#define _XS_OPENSSL_H #define _XS_OPENSSL_H
d_char *xs_md5_hex(const void *input, int size); xs_str *xs_md5_hex(const xs_val *input, int size);
d_char *xs_sha1_hex(const void *input, int size); xs_str *xs_sha1_hex(const xs_val *input, int size);
d_char *xs_sha256_hex(const void *input, int size); xs_str *xs_sha256_hex(const xs_val *input, int size);
d_char *xs_sha256_base64(const void *input, int size); xs_str *xs_sha256_base64(const xs_val *input, int size);
d_char *xs_rsa_genkey(int bits); xs_dict *xs_rsa_genkey(int bits);
d_char *xs_rsa_sign(const char *secret, const char *mem, int size); xs_str *xs_rsa_sign(const char *secret, const char *mem, int size);
int xs_rsa_verify(const char *pubkey, const char *mem, int size, const char *b64sig); int xs_rsa_verify(const char *pubkey, const char *mem, int size, const char *b64sig);
d_char *xs_evp_sign(const char *secret, const char *mem, int size); xs_str *xs_evp_sign(const char *secret, const char *mem, int size);
int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64sig); int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64sig);
@ -23,7 +23,7 @@ int xs_evp_verify(const char *pubkey, const char *mem, int size, const char *b64
#include "openssl/pem.h" #include "openssl/pem.h"
#include "openssl/evp.h" #include "openssl/evp.h"
d_char *xs_md5_hex(const void *input, int size) xs_str *xs_md5_hex(const xs_val *input, int size)
{ {
unsigned char md5[16]; unsigned char md5[16];
MD5_CTX ctx; MD5_CTX ctx;
@ -36,7 +36,7 @@ d_char *xs_md5_hex(const void *input, int size)
} }
d_char *xs_sha1_hex(const void *input, int size) xs_str *xs_sha1_hex(const xs_val *input, int size)
{ {
unsigned char sha1[20]; unsigned char sha1[20];
SHA_CTX ctx; SHA_CTX ctx;
@ -61,7 +61,7 @@ unsigned char *_xs_sha256(const void *input, int size, unsigned char *sha256)
} }
d_char *xs_sha256_hex(const void *input, int size) xs_str *xs_sha256_hex(const xs_val *input, int size)
{ {
unsigned char sha256[32]; unsigned char sha256[32];
@ -71,7 +71,7 @@ d_char *xs_sha256_hex(const void *input, int size)
} }
d_char *xs_sha256_base64(const void *input, int size) xs_str *xs_sha256_base64(const xs_val *input, int size)
{ {
unsigned char sha256[32]; unsigned char sha256[32];
@ -81,12 +81,12 @@ d_char *xs_sha256_base64(const void *input, int size)
} }
d_char *xs_rsa_genkey(int bits) xs_dict *xs_rsa_genkey(int bits)
/* generates an RSA keypair */ /* generates an RSA keypair */
{ {
BIGNUM *bne; BIGNUM *bne;
RSA *rsa; RSA *rsa;
d_char *keypair = NULL; xs_dict *keypair = NULL;
if ((bne = BN_new()) != NULL) { if ((bne = BN_new()) != NULL) {
if (BN_set_word(bne, RSA_F4) == 1) { if (BN_set_word(bne, RSA_F4) == 1) {
@ -119,10 +119,10 @@ d_char *xs_rsa_genkey(int bits)
} }
d_char *xs_rsa_sign(const char *secret, const char *mem, int size) xs_str *xs_rsa_sign(const char *secret, const char *mem, int size)
/* signs a memory block (secret is in PEM format) */ /* signs a memory block (secret is in PEM format) */
{ {
d_char *signature = NULL; xs_str *signature = NULL;
BIO *b; BIO *b;
RSA *rsa; RSA *rsa;
unsigned char *sig; unsigned char *sig;
@ -176,10 +176,10 @@ int xs_rsa_verify(const char *pubkey, const char *mem, int size, const char *b64
} }
d_char *xs_evp_sign(const char *secret, const char *mem, int size) xs_str *xs_evp_sign(const char *secret, const char *mem, int size)
/* signs a memory block (secret is in PEM format) */ /* signs a memory block (secret is in PEM format) */
{ {
d_char *signature = NULL; xs_str *signature = NULL;
BIO *b; BIO *b;
unsigned char *sig; unsigned char *sig;
unsigned int sig_len; unsigned int sig_len;

View file

@ -4,22 +4,22 @@
#define _XS_REGEX_H #define _XS_REGEX_H
d_char *xs_regex_split_n(const char *str, const char *rx, int count); xs_list *xs_regex_split_n(const char *str, const char *rx, int count);
#define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL) #define xs_regex_split(str, rx) xs_regex_split_n(str, rx, XS_ALL)
d_char *xs_regex_match_n(const char *str, const char *rx, int count); xs_list *xs_regex_match_n(const char *str, const char *rx, int count);
#define xs_regex_match(str, rx) xs_regex_match_n(str, rx, XS_ALL) #define xs_regex_match(str, rx) xs_regex_match_n(str, rx, XS_ALL)
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
#include <regex.h> #include <regex.h>
d_char *xs_regex_split_n(const char *str, const char *rx, int count) xs_list *xs_regex_split_n(const char *str, const char *rx, int count)
/* splits str by regex */ /* splits str by regex */
{ {
regex_t re; regex_t re;
regmatch_t rm; regmatch_t rm;
int offset = 0; int offset = 0;
d_char *list = NULL; xs_list *list = NULL;
const char *p; const char *p;
if (regcomp(&re, rx, REG_EXTENDED)) if (regcomp(&re, rx, REG_EXTENDED))
@ -30,11 +30,11 @@ d_char *xs_regex_split_n(const char *str, const char *rx, int count)
while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) { while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) {
/* add first the leading part of the string */ /* add first the leading part of the string */
list = xs_list_append_m(list, p, rm.rm_so); list = xs_list_append_m(list, p, rm.rm_so);
list = xs_str_cat(list, ""); list = xs_insert_m(list, xs_size(list) - 1, "", 1);
/* add now the matched text as the separator */ /* add now the matched text as the separator */
list = xs_list_append_m(list, p + rm.rm_so, rm.rm_eo - rm.rm_so); list = xs_list_append_m(list, p + rm.rm_so, rm.rm_eo - rm.rm_so);
list = xs_str_cat(list, ""); list = xs_insert_m(list, xs_size(list) - 1, "", 1);
/* move forward */ /* move forward */
offset += rm.rm_eo; offset += rm.rm_eo;
@ -51,12 +51,13 @@ d_char *xs_regex_split_n(const char *str, const char *rx, int count)
} }
d_char *xs_regex_match_n(const char *str, const char *rx, int count) xs_list *xs_regex_match_n(const char *str, const char *rx, int count)
/* returns a list with upto count matches */ /* returns a list with upto count matches */
{ {
d_char *list = xs_list_new(); xs_list *list = xs_list_new();
xs *split = NULL; xs *split = NULL;
char *p, *v; xs_list *p;
xs_val *v;
int n = 0; int n = 0;
/* split */ /* split */

View file

@ -8,13 +8,13 @@ typedef struct _xs_set {
int elems; /* number of hash entries */ int elems; /* number of hash entries */
int used; /* number of used hash entries */ int used; /* number of used hash entries */
int *hash; /* hashed offsets */ int *hash; /* hashed offsets */
d_char *list; /* list of stored data */ xs_list *list; /* list of stored data */
} xs_set; } xs_set;
void xs_set_init(xs_set *s); void xs_set_init(xs_set *s);
d_char *xs_set_result(xs_set *s); xs_list *xs_set_result(xs_set *s);
void xs_set_free(xs_set *s); void xs_set_free(xs_set *s);
int xs_set_add(xs_set *s, const char *data); int xs_set_add(xs_set *s, const xs_val *data);
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
@ -33,10 +33,10 @@ void xs_set_init(xs_set *s)
} }
d_char *xs_set_result(xs_set *s) xs_list *xs_set_result(xs_set *s)
/* returns the set as a list and frees it */ /* returns the set as a list and frees it */
{ {
d_char *list = s->list; xs_list *list = s->list;
s->list = NULL; s->list = NULL;
s->hash = xs_free(s->hash); s->hash = xs_free(s->hash);
@ -93,7 +93,7 @@ static int _store_hash(xs_set *s, const char *data, int value)
} }
int xs_set_add(xs_set *s, const char *data) int xs_set_add(xs_set *s, const xs_val *data)
/* adds the data to the set */ /* adds the data to the set */
/* returns: 1 if added, 0 if already there */ /* returns: 1 if added, 0 if already there */
{ {

View file

@ -6,7 +6,7 @@
#include <time.h> #include <time.h>
d_char *xs_str_time(time_t t, const char *fmt, int local); xs_str *xs_str_time(time_t t, const char *fmt, int local);
#define xs_str_localtime(t, fmt) xs_str_time(t, fmt, 1) #define xs_str_localtime(t, fmt) xs_str_time(t, fmt, 1)
#define xs_str_utctime(t, fmt) xs_str_time(t, fmt, 0) #define xs_str_utctime(t, fmt) xs_str_time(t, fmt, 0)
time_t xs_parse_time(const char *str, const char *fmt, int local); time_t xs_parse_time(const char *str, const char *fmt, int local);
@ -15,8 +15,8 @@ time_t xs_parse_time(const char *str, const char *fmt, int local);
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
d_char *xs_str_time(time_t t, const char *fmt, int local) xs_str *xs_str_time(time_t t, const char *fmt, int local)
/* returns a d_char with a formated time */ /* returns a string with a formated time */
{ {
struct tm tm; struct tm tm;
char tmp[64]; char tmp[64];

View file

@ -1 +1 @@
/* f64030f1449390169bd09bdcbb052b1a70f8002a */ /* 86f70ffc75a8c9601adde42146af65c14d41518a */