Skip to content

Commit f9b7cce

Browse files
René Scharfegitster
René Scharfe
authored andcommitted
Add is_regex_special()
Add is_regex_special(), a character class macro for chars that have a special meaning in regular expressions. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8cc3299 commit f9b7cce

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

ctype.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ enum {
1010
A = GIT_ALPHA,
1111
D = GIT_DIGIT,
1212
G = GIT_GLOB_SPECIAL, /* *, ?, [, \\ */
13+
R = GIT_REGEX_SPECIAL, /* $, (, ), +, ., ^, {, | * */
1314
};
1415

1516
unsigned char sane_ctype[256] = {
1617
0, 0, 0, 0, 0, 0, 0, 0, 0, S, S, 0, 0, S, 0, 0, /* 0.. 15 */
1718
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16.. 31 */
18-
S, 0, 0, 0, 0, 0, 0, 0, 0, 0, G, 0, 0, 0, 0, 0, /* 32.. 47 */
19+
S, 0, 0, 0, R, 0, 0, 0, R, R, G, R, 0, 0, R, 0, /* 32.. 47 */
1920
D, D, D, D, D, D, D, D, D, D, 0, 0, 0, 0, 0, G, /* 48.. 63 */
2021
0, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 64.. 79 */
21-
A, A, A, A, A, A, A, A, A, A, A, G, G, 0, 0, 0, /* 80.. 95 */
22+
A, A, A, A, A, A, A, A, A, A, A, G, G, 0, R, 0, /* 80.. 95 */
2223
0, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 96..111 */
23-
A, A, A, A, A, A, A, A, A, A, A, 0, 0, 0, 0, 0, /* 112..127 */
24+
A, A, A, A, A, A, A, A, A, A, A, R, R, 0, 0, 0, /* 112..127 */
2425
/* Nothing in the 128.. range */
2526
};

git-compat-util.h

+2
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,14 @@ extern unsigned char sane_ctype[256];
328328
#define GIT_DIGIT 0x02
329329
#define GIT_ALPHA 0x04
330330
#define GIT_GLOB_SPECIAL 0x08
331+
#define GIT_REGEX_SPECIAL 0x10
331332
#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
332333
#define isspace(x) sane_istest(x,GIT_SPACE)
333334
#define isdigit(x) sane_istest(x,GIT_DIGIT)
334335
#define isalpha(x) sane_istest(x,GIT_ALPHA)
335336
#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
336337
#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
338+
#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
337339
#define tolower(x) sane_case((unsigned char)(x), 0x20)
338340
#define toupper(x) sane_case((unsigned char)(x), 0)
339341

grep.c

+1-8
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,9 @@ void append_grep_pattern(struct grep_opt *opt, const char *pat,
2828
p->next = NULL;
2929
}
3030

31-
static int isregexspecial(int c)
32-
{
33-
return c == '\0' || is_glob_special(c) ||
34-
c == '$' || c == '(' || c == ')' || c == '+' ||
35-
c == '.' || c == '^' || c == '{' || c == '|';
36-
}
37-
3831
static int is_fixed(const char *s)
3932
{
40-
while (!isregexspecial(*s))
33+
while (*s && !is_regex_special(*s))
4134
s++;
4235
return !*s;
4336
}

test-ctype.c

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ static int test_is_glob_special(int c)
2626
return is_glob_special(c);
2727
}
2828

29+
static int test_is_regex_special(int c)
30+
{
31+
return is_regex_special(c);
32+
}
33+
2934
#define DIGIT "0123456789"
3035
#define LOWER "abcdefghijklmnopqrstuvwxyz"
3136
#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -40,6 +45,7 @@ static const struct ctype_class {
4045
{ "isalpha", test_isalpha, LOWER UPPER },
4146
{ "isalnum", test_isalnum, LOWER UPPER DIGIT },
4247
{ "is_glob_special", test_is_glob_special, "*?[\\" },
48+
{ "is_regex_special", test_is_regex_special, "$()*+.?[\\^{|" },
4349
{ NULL }
4450
};
4551

0 commit comments

Comments
 (0)