From d7ddeaf2cab0737cc7bd70a29af771a7ba75ac4d Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 12 Jul 2021 23:51:45 +0100 Subject: [PATCH] Use `slice_sted()` on base-removed slice to calculate offsets instead of using ad-hoc anonymous struct in-line. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 − 小吉 --- include/slice.h | 36 ++++++++++++++++++++++++++++++++++++ src/main.c | 6 ++++++ src/slice.c | 37 +++++++++++++++++++++++++++++++++++++ src/tests/slice.c | 19 +++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 src/slice.c create mode 100644 src/tests/slice.c diff --git a/include/slice.h b/include/slice.h index 0a900d4..5bea5a6 100644 --- a/include/slice.h +++ b/include/slice.h @@ -2,7 +2,10 @@ #ifndef _SLICE_H #define _SLICE_H +#include + #include "ints.h" +#include "macros.h" typedef struct _slice { union { @@ -12,6 +15,39 @@ typedef struct _slice { usize len; } 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)) }) +_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 */ diff --git a/src/main.c b/src/main.c index 0426b2b..0775c82 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,7 @@ #include +#include #include #include @@ -179,12 +180,17 @@ inv_args: if(!comp_slice_of(&needle, current++, &cslice)) { printf("fail: %lu\n", i+1); } else { +#ifdef _DEBUG_MANCALC_OFFSETS struct { usize start, end; } pos = { .start = AS(cslice.ptr - base, usize), }; 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); } } diff --git a/src/slice.c b/src/slice.c new file mode 100644 index 0000000..5e7bcb7 --- /dev/null +++ b/src/slice.c @@ -0,0 +1,37 @@ +#include + +#include + +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; +} diff --git a/src/tests/slice.c b/src/tests/slice.c new file mode 100644 index 0000000..da1e94e --- /dev/null +++ b/src/tests/slice.c @@ -0,0 +1,19 @@ + +#include +#include + +#include + +// 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)