diff --git a/include/comp.h b/include/comp.h index ff3ea60..c0e094e 100644 --- a/include/comp.h +++ b/include/comp.h @@ -3,6 +3,7 @@ #include #include +#include typedef struct comp_aggregate { bool all_matched; @@ -11,13 +12,22 @@ typedef struct comp_aggregate { usize num_results; struct comp_single_result { - map_t* _shared mapping; + const map_t* _shared mapping; bool matched; usize slice_start; } aggr[]; } comp_multires_t; +// Create a `slice_t` from a successful match against `needle`. +_mixin bool comp_slice_of(const map_t*pIN needle, const struct comp_single_result *pIN match, slice_t *pOUT slice) +{ + ifL(match->matched) { + *slice = SLICE(AS(match->mapping->origin,u8*)+match->slice_start, needle->len); + return true; + } else return false; +} + /// Find `needle` in `haystack`, sets the start of `needle`'s `origin` in `haystack`'s to `pos` and returns `true` if a match is found, otherwise returns false. bool cmp_find(const map_t *pIN needle, const map_t *pIN haystack, usize *pOUT pos); /// Check `needle` against `nhaystacks` number of haystack maps, store the offset in `sizes[n]` where `n` is the haystack number. diff --git a/include/slice.h b/include/slice.h new file mode 100644 index 0000000..0a900d4 --- /dev/null +++ b/include/slice.h @@ -0,0 +1,17 @@ +//! Generic pointer slice +#ifndef _SLICE_H +#define _SLICE_H + +#include "ints.h" + +typedef struct _slice { + union { + void* ptr; + u8* bytes; + }; + usize len; +} slice_t; + +#define SLICE(origin, length) ((struct _slice){ .ptr = ((void*)(origin)), .len = ((usize)(length)) }) + +#endif /* _SLICE_H */ diff --git a/include/trace.h b/include/trace.h index 676f30c..4ee67ef 100644 --- a/include/trace.h +++ b/include/trace.h @@ -18,7 +18,7 @@ enum trace_level { #ifdef DEBUG #define _TRACE_LEVEL_DEFAULT TRACE_LEVEL_DEBUG #else -#define _TRACE_LEVEL_DEFAULT TRACE_LEVEL_INFO +#define _TRACE_LEVEL_DEFAULT TRACE_LEVEL_WARN #endif int _t_fprintf(enum trace_level l, FILE* output, const char* msg, ...); diff --git a/src/comp.c b/src/comp.c index c2b65d2..2ff1897 100644 --- a/src/comp.c +++ b/src/comp.c @@ -14,6 +14,7 @@ bool cmp_find(const map_t *pIN needle, const map_t *pIN haystack, usize *pOUT pos) { u8* start; +//TODO: Find sub-substring even if the full match fails u8* substr = memmem(start = haystack->origin, haystack->len, needle->origin, needle->len); if(!substr) return false; diff --git a/src/main.c b/src/main.c index 61d4038..0426b2b 100644 --- a/src/main.c +++ b/src/main.c @@ -171,6 +171,23 @@ inv_args: INFO("Computed result: Suc. Matched %lu / %lu hs. Full match: %s (first failure: %d)", cres, hsn, full_result->all_matched ? "yes" : "no", full_result->idx_first_failure); //TODO: Print out the aggregate (full_result) results (cres) in a reasonable format. + struct comp_single_result* current = full_result->aggr; + for(usize i=0;imapping->origin; + if(!comp_slice_of(&needle, current++, &cslice)) { + printf("fail: %lu\n", i+1); + } else { + struct { + usize start, end; + } pos = { + .start = AS(cslice.ptr - base, usize), + }; + pos.end = pos.start + cslice.len; + printf("match: %lu: %lu -> %lu\n", i+1, pos.start, pos.end); + } + } free(full_result); #endif