|
| 1 | +/* |
| 2 | + * Copyright 2026 Katherine Flavel |
| 3 | + * |
| 4 | + * See LICENCE for the full copyright terms. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <assert.h> |
| 8 | +#include <stdbool.h> |
| 9 | +#include <string.h> |
| 10 | +#include <stdio.h> |
| 11 | + |
| 12 | +#include <re/re.h> |
| 13 | +#include <re/groups.h> |
| 14 | + |
| 15 | +static unsigned failed; |
| 16 | + |
| 17 | +static void |
| 18 | +test(const char *fmt, size_t groupc, const char *groupv[], const char *expected) |
| 19 | +{ |
| 20 | + struct re_pos pos; |
| 21 | + char outs[40]; |
| 22 | + bool r; |
| 23 | + |
| 24 | + assert(fmt != NULL); |
| 25 | + assert(expected != NULL); |
| 26 | + |
| 27 | + if (!re_interpolate_groups(fmt, '$', "<g0>", groupc, groupv, "<ne>", outs, sizeof outs, &pos)) { |
| 28 | + printf("%s/%zu XXX\n", fmt, groupc); |
| 29 | + failed++; |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + failed += r = 0 != strcmp(outs, expected); |
| 34 | + |
| 35 | + printf("%s/%zu => %s%s\n", fmt, groupc, outs, |
| 36 | + r ? " XXX" : ""); |
| 37 | +} |
| 38 | + |
| 39 | +int main(void) { |
| 40 | + const char *gn[] = { "one", "two", "three", "four" }; |
| 41 | + const char **g0 = NULL; |
| 42 | + const char *ga[] = { "1" }; |
| 43 | + const char *gb[] = { "" }; |
| 44 | +// const char *gc[] = { NULL }; // XXX: not permitted |
| 45 | + |
| 46 | + test("", 0, g0, ""); |
| 47 | + test("", 4, gn, ""); |
| 48 | + |
| 49 | + test("x", 0, g0, "x"); |
| 50 | + test("x", 4, gn, "x"); |
| 51 | + |
| 52 | + test("\001", 0, g0, "\001"); |
| 53 | + test("\001", 4, gn, "\001"); |
| 54 | + |
| 55 | + test("$0", 0, gn, "<g0>"); |
| 56 | + test("x$000000000000000000000x", 0, gn, "x<g0>x"); |
| 57 | + test("x$000000000000000000001x", 1, gn, "xone"); |
| 58 | + test("x$100000000000000000000x", 1, gn, "x<ne>"); |
| 59 | + |
| 60 | + test("$$$1$1$2$1$3$4$3$2$1$$$$", 4, gn, "$oneonetwoonethreefourthreetwoone$$"); |
| 61 | + |
| 62 | + test("xyz_$1..$0003;$3,$$.$1-$4=$123", 4, gn, "xyz_one..three;three,$.one-four=<ne>"); |
| 63 | + test("xyz_$1..$0003;$3,$$.$1-$4=$123", 3, gn, "xyz_one..three;three,$.one-<ne>=<ne>"); |
| 64 | + test("xyz_$1..$0003;$3,$$.$1-$4=$123", 2, gn, "xyz_one..<ne>;<ne>,$.one-<ne>=<ne>"); |
| 65 | + test("xyz_$1..$0003;$3,$$.$1-$4=$123", 1, gn, "xyz_one..<ne>;<ne>,$.one-<ne>=<ne>"); |
| 66 | + test("xyz_$1..$0003;$3,$$.$1-$4=$123", 0, g0, "xyz_<ne>..<ne>;<ne>,$.<ne>-<ne>=<ne>"); |
| 67 | + test("xyz_$1..$0003;$3,$$.$1-$4=$123", 1, ga, "xyz_1..<ne>;<ne>,$.1-<ne>=<ne>"); |
| 68 | + test("xyz_$1..$0003;$3,$$.$1-$4=$123", 1, gb, "xyz_..<ne>;<ne>,$.-<ne>=<ne>"); |
| 69 | +// test("xyz_$1..$0003;$3,$$.$1-$4=$123", 0, gc, "xyz_<ne>..<ne>;<ne>,$.<ne>-<ne>=<ne>"); |
| 70 | + |
| 71 | + return failed; |
| 72 | +} |
| 73 | + |
0 commit comments