Added basic reporting of haystack match offsets and failures.

Change default trace level in release builds from `INFO` to `WARN`.

Fortune for naka's current commit: Small blessing − 小吉
master
Avril 3 years ago
parent 2ed48ec977
commit 4465050a29
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -3,6 +3,7 @@
#include <macros.h>
#include <ints.h>
#include <slice.h>
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.

@ -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 */

@ -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, ...);

@ -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;

@ -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;i<cres;i++)
{
slice_t cslice;
const void* base = current->mapping->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

Loading…
Cancel
Save