Use `slice_sted()` on base-removed slice to calculate offsets instead of using ad-hoc anonymous struct in-line.
I don"t know if this is more efficient or not, but it looks cleaner. Define `_DEBUG_MANCALC_OFFSETS` to use the previous offset calculation code. Fortune for naka's current commit: Small blessing − 小吉master
parent
4465050a29
commit
d7ddeaf2ca
@ -0,0 +1,37 @@
|
||||
#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;
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
|
||||
#include <macros.h>
|
||||
#include <tests.h>
|
||||
|
||||
#include <slice.h>
|
||||
|
||||
// Internal `slice.c` function prototyped.
|
||||
usize _slice_str_max_sz();
|
||||
|
||||
DEFTEST(slice_static_str_len)
|
||||
{
|
||||
const usize func = _slice_str_max_sz();
|
||||
INFO("Function reports: %lu", func);
|
||||
INFO("Constant is: %lu", SLICE_STR_MAX_SIZE);
|
||||
TEST_ASSERT( (func == SLICE_STR_MAX_SIZE) );
|
||||
|
||||
return TEST_OK;
|
||||
}
|
||||
RUNTEST_DEBUG(slice_static_str_len)
|
Loading…
Reference in new issue