Skip to content

Commit 4546738

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
Unlocalized isspace and friends
Do our own ctype.h, just to get the sane semantics: we want locale-independence, _and_ we want the right signed behaviour. Plus we only use a very small subset of ctype.h anyway (isspace, isalpha, isdigit and isalnum). Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent d402d55 commit 4546738

File tree

16 files changed

+51
-14
lines changed

16 files changed

+51
-14
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ LIB_OBJS = \
159159
object.o pack-check.o patch-delta.o path.o pkt-line.o \
160160
quote.o read-cache.o refs.o run-command.o \
161161
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
162-
tag.o tree.o usage.o config.o environment.o $(DIFF_OBJS)
162+
tag.o tree.o usage.o config.o environment.o ctype.o \
163+
$(DIFF_OBJS)
163164

164165
LIBS = $(LIB_FILE)
165166
LIBS += -lz

apply.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* This applies patches on top of some (arbitrary) version of the SCM.
77
*
88
*/
9-
#include <ctype.h>
109
#include <fnmatch.h>
1110
#include "cache.h"
1211

cache.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,30 @@ extern int git_config_bool(const char *, const char *);
387387
extern char git_default_email[MAX_GITNAME];
388388
extern char git_default_name[MAX_GITNAME];
389389

390+
/* Sane ctype - no locale, and works with signed chars */
391+
#undef isspace
392+
#undef isdigit
393+
#undef isalpha
394+
#undef isalnum
395+
#undef tolower
396+
#undef toupper
397+
extern unsigned char sane_ctype[256];
398+
#define GIT_SPACE 0x01
399+
#define GIT_DIGIT 0x02
400+
#define GIT_ALPHA 0x04
401+
#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
402+
#define isspace(x) sane_istest(x,GIT_SPACE)
403+
#define isdigit(x) sane_istest(x,GIT_DIGIT)
404+
#define isalpha(x) sane_istest(x,GIT_ALPHA)
405+
#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
406+
#define tolower(x) sane_case((unsigned char)(x), 0x20)
407+
#define toupper(x) sane_case((unsigned char)(x), 0)
408+
409+
static inline int sane_case(int x, int high)
410+
{
411+
if (sane_istest(x, GIT_ALPHA))
412+
x = (x & ~0x20) | high;
413+
return x;
414+
}
415+
390416
#endif /* CACHE_H */

commit-tree.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <pwd.h>
99
#include <time.h>
10-
#include <ctype.h>
1110

1211
#define BLOCKING (1ul << 14)
1312

commit.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <ctype.h>
21
#include "tag.h"
32
#include "commit.h"
43
#include "cache.h"

config.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <ctype.h>
21

32
#include "cache.h"
43

convert-objects.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#define _XOPEN_SOURCE /* glibc2 needs this */
22
#include <time.h>
3-
#include <ctype.h>
43
#include "cache.h"
54

65
struct entry {

ctype.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Sane locale-independent, ASCII ctype.
3+
*
4+
* No surprises, and works with signed and unsigned chars.
5+
*/
6+
#include "cache.h"
7+
8+
#define SS GIT_SPACE
9+
#define AA GIT_ALPHA
10+
#define DD GIT_DIGIT
11+
12+
unsigned char sane_ctype[256] = {
13+
0, 0, 0, 0, 0, 0, 0, 0, 0, SS, SS, 0, 0, SS, 0, 0, /* 0-15 */
14+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16-15 */
15+
SS, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 32-15 */
16+
DD, DD, DD, DD, DD, DD, DD, DD, DD, DD, 0, 0, 0, 0, 0, 0, /* 48-15 */
17+
0, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, /* 64-15 */
18+
AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, 0, 0, 0, 0, 0, /* 80-15 */
19+
0, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, /* 96-15 */
20+
AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, AA, 0, 0, 0, 0, 0, /* 112-15 */
21+
/* Nothing in the 128.. range */
22+
};
23+

date.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* Copyright (C) Linus Torvalds, 2005
55
*/
66

7-
#include <ctype.h>
87
#include <time.h>
98

109
#include "cache.h"

diff-tree.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <ctype.h>
21
#include "cache.h"
32
#include "diff.h"
43
#include "commit.h"

0 commit comments

Comments
 (0)