added re9_subst() and sample for it
[libre9.git] / src / test6.c
blob6082c208d048b0859bddabc005bd5b4ecb97577f
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "libre9/re9.h"
8 int main (int argc, char *argv[]) {
9 re9_prog_t *p;
10 const char *errmsg;
11 if (argc < 4) {
12 fprintf(stderr, "usage: %s regexp substr string string...\n", argv[0]);
13 return 1;
15 p = re9_compile(argv[1], RE9_FLAG_NONUTF8, &errmsg);
16 if (p == NULL) { fprintf(stderr, "FATAL: invalid regexp '%s': %s\n", argv[1], errmsg); return 1; }
17 for (int f = 3; f < argc; ++f) {
18 re9_sub_t rs[10];
19 if (f > 3) printf("===============================\n");
20 if (re9_execute(p, RE9_FLAG_NONUTF8, argv[f], rs, 10)) {
21 static char dst[8192];
22 int sz = re9_subst(dst, sizeof(dst), argv[2], rs, re9_nsub(p));
23 printf(" nsub=%d; sub %s -> <%s> (%d)\n", re9_nsub(p), argv[2], dst, sz);
26 re9_free(p);
27 return 0;