dir.c: Avoid c99 array initialization
The following syntax: char foo[] = { [0] = 1, [7] = 2, [15] = 3 }; is a c99 construct which some compilers do not support even though they support other c99 constructs. This construct can be avoided by folding these 'special' test cases into the sane_ctype array and making use of the related infrastructure. Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Brandon Casey committed
Aug 28, 2008 at 17:47 UTC
63e8aea74e992bd8667e35a0e646110acc1f0d7c
3 files changed
+10
-16
ctype.c
+6
-4
@@ -9,18 +9,20 @@
9
#undef SS
10
#undef AA
11
#undef DD
12
+#undef GS
13
14
#define SS GIT_SPACE
15
#define AA GIT_ALPHA
16
#define DD GIT_DIGIT
17
+#define GS GIT_SPECIAL /* \0, *, ?, [, \\ */
18
19
unsigned char sane_ctype[256] = {
18
- 0, 0, 0, 0, 0, 0, 0, 0, 0, SS, SS, 0, 0, SS, 0, 0, /* 0-15 */
20
+ GS, 0, 0, 0, 0, 0, 0, 0, 0, SS, SS, 0, 0, SS, 0, 0, /* 0-15 */
21
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16-15 */
20
- SS, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 32-15 */
21
- DD, DD, DD, DD, DD, DD, DD, DD, DD, DD, 0, 0, 0, 0, 0, 0, /* 48-15 */
22
+ SS, 0, 0, 0, 0, 0, 0, 0, 0, 0, GS, 0, 0, 0, 0, 0, /* 32-15 */
23
+ DD, DD, DD, DD, DD, DD, DD, DD, DD, DD, 0, 0, 0, 0, 0, GS, /* 48-15 */
24
0, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, /* 64-15 */
23
- AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, 0, 0, 0, 0, 0, /* 80-15 */
25
+ AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, GS, GS, 0, 0, 0, /* 80-15 */
26
0, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, /* 96-15 */
27
AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, 0, 0, 0, 0, 0, /* 112-15 */
28
/* Nothing in the 128.. range */
dir.c
+2
-12
@@ -52,11 +52,6 @@ int common_prefix(const char **pathspec)
52
return prefix;
53
}
54
55
-static inline int special_char(unsigned char c1)
56
-{
57
- return !c1 || c1 == '*' || c1 == '[' || c1 == '?' || c1 == '\\';
58
-}
59
-
55
/*
56
* Does 'match' matches the given name?
57
* A match is found if
@@ -80,7 +75,7 @@ static int match_one(const char *match, const char *name, int namelen)
75
for (;;) {
76
unsigned char c1 = *match;
77
unsigned char c2 = *name;
83
- if (special_char(c1))
78
+ if (isspecial(c1))
79
break;
80
if (c1 != c2)
81
return 0;
@@ -680,17 +675,12 @@ static int cmp_name(const void *p1, const void *p2)
675
*/
676
static int simple_length(const char *match)
677
{
683
- const char special[256] = {
684
- [0] = 1, ['?'] = 1,
685
- ['\\'] = 1, ['*'] = 1,
686
- ['['] = 1
687
- };
678
int len = -1;
679
680
for (;;) {
681
unsigned char c = *match++;
682
len++;
693
- if (special[c])
683
+ if (isspecial(c))
684
return len;
685
}
686
}
git-compat-util.h
+2
@@ -329,11 +329,13 @@ extern unsigned char sane_ctype[256];
329
#define GIT_SPACE 0x01
330
#define GIT_DIGIT 0x02
331
#define GIT_ALPHA 0x04
332
+#define GIT_SPECIAL 0x08
333
#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
334
#define isspace(x) sane_istest(x,GIT_SPACE)
335
#define isdigit(x) sane_istest(x,GIT_DIGIT)
336
#define isalpha(x) sane_istest(x,GIT_ALPHA)
337
#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
338
+#define isspecial(x) sane_istest(x,GIT_SPECIAL)
339
#define tolower(x) sane_case((unsigned char)(x), 0x20)
340
#define toupper(x) sane_case((unsigned char)(x), 0)
341