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
Avril 3 years ago
parent 4465050a29
commit d7ddeaf2ca
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -2,7 +2,10 @@
#ifndef _SLICE_H #ifndef _SLICE_H
#define _SLICE_H #define _SLICE_H
#include <limits.h>
#include "ints.h" #include "ints.h"
#include "macros.h"
typedef struct _slice { typedef struct _slice {
union { union {
@ -12,6 +15,39 @@ typedef struct _slice {
usize len; usize len;
} slice_t; } slice_t;
#define SLICE_MAX ((struct _slice){ .ptr = AS(UINTPTR_MAX, void*), .len = SIZE_MAX })
#define SLICE(origin, length) ((struct _slice){ .ptr = ((void*)(origin)), .len = ((usize)(length)) }) #define SLICE(origin, length) ((struct _slice){ .ptr = ((void*)(origin)), .len = ((usize)(length)) })
_mixin usize slice_end(const slice_t*pIN slice)
{
return AS(slice->bytes + slice->len, usize);
}
_mixin usize slice_start(const slice_t*pIN slice)
{
return AS(slice->bytes, usize);
}
_mixin struct { usize start, end; } slice_sted(const slice_t*pIN slice)
{
return (var(slice_sted(NULL))){
.start = slice_start(slice),
.end = slice_end(slice),
};
}
// Format string for slices
#define SLICE_FORMAT "%lu..%lu"
// Expects a pointer to a `slice_t`
#define SLICE_FORMAT_ARGS(slice) slice_start(slice), slice_end(slice)
#define SLICE_STR_MAX_SIZE 42lu
usize slice_strn(const slice_t*pIN slice, usize len, char str[static pOUT len]);
usize slice_str_sz(const slice_t*pIN slice);
// Pointer returned from this function is TLS static and will change on each call.
const char* slice_strs(const slice_t*pIN slice);
// Pointer returned from this function must be free'd
char* slice_str(const slice_t*pIN slice);
#endif /* _SLICE_H */ #endif /* _SLICE_H */

@ -13,6 +13,7 @@
#include <tests.h> #include <tests.h>
#include <slice.h>
#include <map.h> #include <map.h>
#include <comp.h> #include <comp.h>
@ -179,12 +180,17 @@ inv_args:
if(!comp_slice_of(&needle, current++, &cslice)) { if(!comp_slice_of(&needle, current++, &cslice)) {
printf("fail: %lu\n", i+1); printf("fail: %lu\n", i+1);
} else { } else {
#ifdef _DEBUG_MANCALC_OFFSETS
struct { struct {
usize start, end; usize start, end;
} pos = { } pos = {
.start = AS(cslice.ptr - base, usize), .start = AS(cslice.ptr - base, usize),
}; };
pos.end = pos.start + cslice.len; pos.end = pos.start + cslice.len;
#else
let pos = slice_sted((slice_t[]){ SLICE(AS(cslice.ptr - base, usize), cslice.len)});
#endif
INFO("Mapped region slice for %lu, pre-transform (at %p): " SLICE_FORMAT ", len: %lu", i+1, base, SLICE_FORMAT_ARGS(&cslice), cslice.len);
printf("match: %lu: %lu -> %lu\n", i+1, pos.start, pos.end); printf("match: %lu: %lu -> %lu\n", i+1, pos.start, pos.end);
} }
} }

@ -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…
Cancel
Save