Make pikevm a lib

This commit is contained in:
2026-01-24 07:18:32 -06:00
parent b6c04e4b85
commit 1cc16538a4

View File

@@ -1,9 +1,12 @@
/* /*
Copyright 2007-2009 Russ Cox. All Rights Reserved. Copyright 2007-2009 Russ Cox. All Rights Reserved.
Copyright 2020-2025 Kyryl Melekhin. All Rights Reserved. Copyright 2020-2025 Kyryl Melekhin. All Rights Reserved.
Copyright 2026-2026 Pedro E. Diaz. All Rights Reserved.
Use of this source code is governed by a BSD-style Use of this source code is governed by a BSD-style
*/ */
#ifdef STB_PIKEVM_IMPLEMENTATION
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -382,7 +385,7 @@ static int compilecode(const char *re_loc, rcode *prog, int sizecode)
return capc ? -1 : 0; return capc ? -1 : 0;
} }
static int re_sizecode(const char *re, int *nsub) int re_sizecode(const char *re, int *nsub)
{ {
rcode dummyprog; rcode dummyprog;
dummyprog.unilen = 4; dummyprog.unilen = 4;
@@ -640,53 +643,4 @@ static int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp)
} }
return 0; return 0;
} }
#endif
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("usage: <regex> <str...> <str...> ...\n");
return 0;
}
int sub_els;
int sz = re_sizecode(argv[1], &sub_els) * sizeof(int);
printf("Precalculated size: %d\n", sz);
if (sz < 0) {
printf("Error in re_sizecode\n");
return 1;
}
char code[sizeof(rcode)+sz];
rcode *_code = (rcode*)code;
if (reg_comp(_code, argv[1], sub_els)) {
printf("Error in reg_comp\n");
return 1;
}
re_dumpcode(_code);
if (argc > 2) {
sub_els = (sub_els + 1) * 2;
const char *sub[sub_els];
for (int i = 2; i < argc; i++) {
printf("input bytelen: %ld\n", strlen(argv[i]));
clock_t start_time = clock();
sz = re_pikevm(_code, argv[i], sub, sub_els);
double elapsed_time = (double)(clock() - start_time) / CLOCKS_PER_SEC;
printf("Done in %f seconds\n", elapsed_time);
if (!sz)
{ printf("-nomatch-\n"); continue; }
for (int l = 0; l < sub_els; l+=2) {
printf("(");
if (sub[l] == NULL || sub[l+1] == NULL)
printf("?");
else
printf("%d", (int)(sub[l] - argv[i]));
printf(",");
if (sub[l+1] == NULL || sub[l] == NULL)
printf("?");
else
printf("%d", (int)(sub[l+1] - argv[i]));
printf(")");
}
printf("\n");
}
}
return 0;
}