Skip to content

Commit d8acfe1

Browse files
pcloudsgitster
authored andcommitted
test-regex: expose full regcomp() to the command line
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 949782d commit d8acfe1

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

test-regex.c

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
#include "git-compat-util.h"
2+
#include "gettext.h"
3+
4+
struct reg_flag {
5+
const char *name;
6+
int flag;
7+
};
8+
9+
static struct reg_flag reg_flags[] = {
10+
{ "EXTENDED", REG_EXTENDED },
11+
{ "NEWLINE", REG_NEWLINE },
12+
{ "ICASE", REG_ICASE },
13+
{ "NOTBOL", REG_NOTBOL },
14+
#ifdef REG_STARTEND
15+
{ "STARTEND", REG_STARTEND },
16+
#endif
17+
{ NULL, 0 }
18+
};
219

320
static int test_regex_bug(void)
421
{
@@ -21,8 +38,38 @@ static int test_regex_bug(void)
2138

2239
int main(int argc, char **argv)
2340
{
41+
const char *pat;
42+
const char *str;
43+
int flags = 0;
44+
regex_t r;
45+
regmatch_t m[1];
46+
2447
if (argc == 2 && !strcmp(argv[1], "--bug"))
2548
return test_regex_bug();
26-
else
27-
usage("test-regex --bug");
49+
else if (argc < 3)
50+
usage("test-regex --bug\n"
51+
"test-regex <pattern> <string> [<options>]");
52+
53+
argv++;
54+
pat = *argv++;
55+
str = *argv++;
56+
while (*argv) {
57+
struct reg_flag *rf;
58+
for (rf = reg_flags; rf->name; rf++)
59+
if (!strcmp(*argv, rf->name)) {
60+
flags |= rf->flag;
61+
break;
62+
}
63+
if (!rf->name)
64+
die("do not recognize %s", *argv);
65+
argv++;
66+
}
67+
git_setup_gettext();
68+
69+
if (regcomp(&r, pat, flags))
70+
die("failed regcomp() for pattern '%s'", pat);
71+
if (regexec(&r, str, 1, m, 0))
72+
return 1;
73+
74+
return 0;
2875
}

0 commit comments

Comments
 (0)