From ae89f342b51cb4ef71f18e89b74585cd61b05a60 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 17 Dec 2025 16:20:43 +0100 Subject: First commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libautomata_compile_mp_automaton.c | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 libautomata_compile_mp_automaton.c (limited to 'libautomata_compile_mp_automaton.c') diff --git a/libautomata_compile_mp_automaton.c b/libautomata_compile_mp_automaton.c new file mode 100644 index 0000000..b9e8739 --- /dev/null +++ b/libautomata_compile_mp_automaton.c @@ -0,0 +1,44 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +LIBAUTOMATA_MP_AUTOMATON * +libautomata_compile_mp_automaton(const void *pattern, size_t length, size_t elemsize) +{ + size_t i = 0, j = SIZE_MAX; + size_t size = sizeof_mp_automaton(length, elemsize); + LIBAUTOMATA_MP_AUTOMATON *ret = malloc(size); + + if (!ret) + return NULL; + + ret->position = 0; + ret->length = length; + ret->elemsize = elemsize; + memcpy((void *)&ret->next[length + 1U], pattern, length * elemsize); + +#define INTRINSIC(TYPE)\ + (((const TYPE *)pattern)[i] == ((const TYPE *)pattern)[j]) + +#define MEMCMP(WIDTH)\ + (!memcmp(&((const char *)pattern)[i * (WIDTH)], &((const char *)pattern)[j * (WIDTH)], (WIDTH))) + +#define IMPLEMENT(EQ, EQ_PARAM, CASE)\ + while (i < length) {\ + if (j != SIZE_MAX && !(EQ(EQ_PARAM)))\ + j = ret->next[j];\ + i++;\ + j++;\ + CASE:\ + ret->next[i] = j;\ + }\ + return ret + + switch (elemsize) { + IMPLEMENT(INTRINSIC, uint8_t, case sizeof(uint8_t)); + IMPLEMENT(INTRINSIC, uint16_t, case sizeof(uint16_t)); + IMPLEMENT(INTRINSIC, uint32_t, case sizeof(uint32_t)); + IMPLEMENT(INTRINSIC, uint64_t, case sizeof(uint64_t)); + IMPLEMENT(MEMCMP, elemsize, default); + } +} -- cgit v1.2.3-70-g09d2