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>
This commit is contained in:
René Scharfe 2009-01-17 16:50:37 +01:00 коммит произвёл Junio C Hamano
Родитель 8cc3299262
Коммит f9b7cce61c
4 изменённых файлов: 13 добавлений и 11 удалений

Просмотреть файл

@ -10,16 +10,17 @@ enum {
A = GIT_ALPHA, A = GIT_ALPHA,
D = GIT_DIGIT, D = GIT_DIGIT,
G = GIT_GLOB_SPECIAL, /* *, ?, [, \\ */ G = GIT_GLOB_SPECIAL, /* *, ?, [, \\ */
R = GIT_REGEX_SPECIAL, /* $, (, ), +, ., ^, {, | * */
}; };
unsigned char sane_ctype[256] = { unsigned char sane_ctype[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, S, S, 0, 0, S, 0, 0, /* 0.. 15 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, S, S, 0, 0, S, 0, 0, /* 0.. 15 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16.. 31 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16.. 31 */
S, 0, 0, 0, 0, 0, 0, 0, 0, 0, G, 0, 0, 0, 0, 0, /* 32.. 47 */ S, 0, 0, 0, R, 0, 0, 0, R, R, G, R, 0, 0, R, 0, /* 32.. 47 */
D, D, D, D, D, D, D, D, D, D, 0, 0, 0, 0, 0, G, /* 48.. 63 */ D, D, D, D, D, D, D, D, D, D, 0, 0, 0, 0, 0, G, /* 48.. 63 */
0, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 64.. 79 */ 0, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 64.. 79 */
A, A, A, A, A, A, A, A, A, A, A, G, G, 0, 0, 0, /* 80.. 95 */ A, A, A, A, A, A, A, A, A, A, A, G, G, 0, R, 0, /* 80.. 95 */
0, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 96..111 */ 0, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 96..111 */
A, A, A, A, A, A, A, A, A, A, A, 0, 0, 0, 0, 0, /* 112..127 */ A, A, A, A, A, A, A, A, A, A, A, R, R, 0, 0, 0, /* 112..127 */
/* Nothing in the 128.. range */ /* Nothing in the 128.. range */
}; };

Просмотреть файл

@ -328,12 +328,14 @@ extern unsigned char sane_ctype[256];
#define GIT_DIGIT 0x02 #define GIT_DIGIT 0x02
#define GIT_ALPHA 0x04 #define GIT_ALPHA 0x04
#define GIT_GLOB_SPECIAL 0x08 #define GIT_GLOB_SPECIAL 0x08
#define GIT_REGEX_SPECIAL 0x10
#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0) #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
#define isspace(x) sane_istest(x,GIT_SPACE) #define isspace(x) sane_istest(x,GIT_SPACE)
#define isdigit(x) sane_istest(x,GIT_DIGIT) #define isdigit(x) sane_istest(x,GIT_DIGIT)
#define isalpha(x) sane_istest(x,GIT_ALPHA) #define isalpha(x) sane_istest(x,GIT_ALPHA)
#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT) #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL) #define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
#define tolower(x) sane_case((unsigned char)(x), 0x20) #define tolower(x) sane_case((unsigned char)(x), 0x20)
#define toupper(x) sane_case((unsigned char)(x), 0) #define toupper(x) sane_case((unsigned char)(x), 0)

9
grep.c
Просмотреть файл

@ -28,16 +28,9 @@ void append_grep_pattern(struct grep_opt *opt, const char *pat,
p->next = NULL; p->next = NULL;
} }
static int isregexspecial(int c)
{
return c == '\0' || is_glob_special(c) ||
c == '$' || c == '(' || c == ')' || c == '+' ||
c == '.' || c == '^' || c == '{' || c == '|';
}
static int is_fixed(const char *s) static int is_fixed(const char *s)
{ {
while (!isregexspecial(*s)) while (*s && !is_regex_special(*s))
s++; s++;
return !*s; return !*s;
} }

Просмотреть файл

@ -26,6 +26,11 @@ static int test_is_glob_special(int c)
return is_glob_special(c); return is_glob_special(c);
} }
static int test_is_regex_special(int c)
{
return is_regex_special(c);
}
#define DIGIT "0123456789" #define DIGIT "0123456789"
#define LOWER "abcdefghijklmnopqrstuvwxyz" #define LOWER "abcdefghijklmnopqrstuvwxyz"
#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ" #define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@ -40,6 +45,7 @@ static const struct ctype_class {
{ "isalpha", test_isalpha, LOWER UPPER }, { "isalpha", test_isalpha, LOWER UPPER },
{ "isalnum", test_isalnum, LOWER UPPER DIGIT }, { "isalnum", test_isalnum, LOWER UPPER DIGIT },
{ "is_glob_special", test_is_glob_special, "*?[\\" }, { "is_glob_special", test_is_glob_special, "*?[\\" },
{ "is_regex_special", test_is_regex_special, "$()*+.?[\\^{|" },
{ NULL } { NULL }
}; };