added re9_subst() and sample for it
[libre9.git] / src / test3.c
blobc25406ceb93bc849cb487bed9e10163ed92d7d2c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
6 #include "libre9/re9.h"
9 static const char text[] = "test\0for zero";
12 static void dump_matches (const char *str, const re9_sub_t *rs, int ms) {
13 for (int c = 0; c < 10; ++c) {
14 if (rs[c].sp != NULL && rs[c].ep != NULL) {
15 fprintf(stdout, " %d:(%d,%d) <", c, rs[c].sp-str, rs[c].ep-str);
16 fwrite(rs[c].sp, rs[c].ep-rs[c].sp, 1, stdout);
17 fputc('>', stdout);
20 fputc('\n', stdout);
24 int main (int argc, char *argv[]) {
25 re9_sub_t rs[10];
26 re9_prog_t *p0, *p1, *p2;
27 const char *errmsg;
28 /* */
29 p0 = re9_compile("t\\zfor", RE9_FLAG_NONUTF8, &errmsg);
30 if (p0 == NULL) { fprintf(stderr, "FATAL: invalid regexp: %s\n", errmsg); exit(2); }
31 p1 = re9_compile("\\zfor", RE9_FLAG_NONUTF8, &errmsg);
32 if (p1 == NULL) { fprintf(stderr, "FATAL: invalid regexp: %s\n", errmsg); exit(2); }
33 p2 = re9_compile("[a\\z]for", RE9_FLAG_NONUTF8, &errmsg);
34 if (p1 == NULL) { fprintf(stderr, "FATAL: invalid regexp: %s\n", errmsg); exit(2); }
35 /* */
36 printf("=============================== (%u)\n", sizeof(text));
37 rs[0].sp = text;
38 rs[0].ep = text+sizeof(text);
39 if (re9_execute(p0, RE9_FLAG_NONUTF8|RE9_FLAG_MT0_RANGE, text, rs, 10) > 0) dump_matches(text, rs, 10);
40 printf("=============================== (%u)\n", sizeof(text));
41 rs[0].sp = text;
42 rs[0].ep = text+sizeof(text);
43 if (re9_execute(p1, RE9_FLAG_NONUTF8|RE9_FLAG_MT0_RANGE, text, rs, 10) > 0) dump_matches(text, rs, 10);
44 printf("=============================== (%u)\n", sizeof(text));
45 rs[0].sp = text;
46 rs[0].ep = text+sizeof(text);
47 if (re9_execute(p2, RE9_FLAG_NONUTF8|RE9_FLAG_MT0_RANGE, text, rs, 10) > 0) dump_matches(text, rs, 10);
48 /* */
49 re9_free(p2);
50 re9_free(p1);
51 re9_free(p0);
52 /* */
53 return 0;