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 <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jan 17, 2009 at 16:50 UTC
f9b7cce61cbd19c99e89b859b5909f0741111185
4 files changed
+13
-11
ctype.c
+4
-3
@@ -10,16 +10,17 @@ enum {
10
A = GIT_ALPHA,
11
D = GIT_DIGIT,
12
G = GIT_GLOB_SPECIAL, /* *, ?, [, \\ */
13
+ R = GIT_REGEX_SPECIAL, /* $, (, ), +, ., ^, {, | * */
14
};
15
16
unsigned char sane_ctype[256] = {
17
0, 0, 0, 0, 0, 0, 0, 0, 0, S, S, 0, 0, S, 0, 0, /* 0.. 15 */
18
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 */
20
D, D, D, D, D, D, D, D, D, D, 0, 0, 0, 0, 0, G, /* 48.. 63 */
21
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 */
23
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 */
25
/* Nothing in the 128.. range */
26
};
git-compat-util.h
+2
@@ -328,12 +328,14 @@ extern unsigned char sane_ctype[256];
328
#define GIT_DIGIT 0x02
329
#define GIT_ALPHA 0x04
330
#define GIT_GLOB_SPECIAL 0x08
331
+#define GIT_REGEX_SPECIAL 0x10
332
#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
333
#define isspace(x) sane_istest(x,GIT_SPACE)
334
#define isdigit(x) sane_istest(x,GIT_DIGIT)
335
#define isalpha(x) sane_istest(x,GIT_ALPHA)
336
#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
337
#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)
339
#define tolower(x) sane_case((unsigned char)(x), 0x20)
340
#define toupper(x) sane_case((unsigned char)(x), 0)
341
grep.c
+1
-8
@@ -28,16 +28,9 @@ void append_grep_pattern(struct grep_opt *opt, const char *pat,
28
p->next = NULL;
29
}
30
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
-
31
static int is_fixed(const char *s)
32
{
40
- while (!isregexspecial(*s))
33
+ while (*s && !is_regex_special(*s))
34
s++;
35
return !*s;
36
}
test-ctype.c
+6
@@ -26,6 +26,11 @@ static int test_is_glob_special(int c)
26
return is_glob_special(c);
27
}
28
29
+static int test_is_regex_special(int c)
30
+{
31
+ return is_regex_special(c);
32
+}
33
+
34
#define DIGIT "0123456789"
35
#define LOWER "abcdefghijklmnopqrstuvwxyz"
36
#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -40,6 +45,7 @@ static const struct ctype_class {
45
{ "isalpha", test_isalpha, LOWER UPPER },
46
{ "isalnum", test_isalnum, LOWER UPPER DIGIT },
47
{ "is_glob_special", test_is_glob_special, "*?[\\" },
48
+ { "is_regex_special", test_is_regex_special, "$()*+.?[\\^{|" },
49
{ NULL }
50
};
51