You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
naka/src/slice.c

38 lines
892 B

#include <stdio.h>
#include <slice.h>
usize slice_strn(const slice_t*pIN slice, usize len, char str[static pOUT len])
{
return snprintf(str, len, SLICE_FORMAT, SLICE_FORMAT_ARGS(slice));
}
usize slice_str_sz(const slice_t*pIN slice)
{
return snprintf(NULL, 0, SLICE_FORMAT, SLICE_FORMAT_ARGS(slice));
}
usize _slice_str_max_sz()
{
const slice_t max = SLICE_MAX;
return snprintf(NULL, 0, SLICE_FORMAT, SLICE_FORMAT_ARGS(&max));
}
const char* slice_strs(const slice_t*pIN slice)
{
static _Thread_local char buf[SLICE_STR_MAX_SIZE+1];
debug_assert(sizeof(buf) == SLICE_STR_MAX_SIZE+1);
buf[snprintf(buf, sizeof(buf), SLICE_FORMAT, SLICE_FORMAT_ARGS(slice))] = 0;
return buf;
}
char* slice_str(const slice_t*pIN slice)
{
usize ln = slice_str_sz(slice)+1;
char *buf = malloc(ln);
usize _out = slice_strn(slice, ln, buf);
debug_assert(_out == ln);
IGNORE(_out);
return buf;
}