Merged revisions 76156 via svnmerge from
[python/dscho.git] / Tools / scripts / make_ctype.py
blob359d6b3160f24ebb47803308ded34c8b00433d29
1 """Script that generates the ctype.h-replacement in stringobject.c."""
3 NAMES = ("LOWER", "UPPER", "ALPHA", "DIGIT", "XDIGIT", "ALNUM", "SPACE")
5 print("""
6 #define FLAG_LOWER 0x01
7 #define FLAG_UPPER 0x02
8 #define FLAG_ALPHA (FLAG_LOWER|FLAG_UPPER)
9 #define FLAG_DIGIT 0x04
10 #define FLAG_ALNUM (FLAG_ALPHA|FLAG_DIGIT)
11 #define FLAG_SPACE 0x08
12 #define FLAG_XDIGIT 0x10
14 static unsigned int ctype_table[256] = {""")
16 for i in range(128):
17 c = chr(i)
18 flags = []
19 for name in NAMES:
20 if name in ("ALPHA", "ALNUM"):
21 continue
22 if name == "XDIGIT":
23 method = lambda: c.isdigit() or c.upper() in "ABCDEF"
24 else:
25 method = getattr(c, "is" + name.lower())
26 if method():
27 flags.append("FLAG_" + name)
28 rc = repr(c)
29 if c == '\v':
30 rc = "'\\v'"
31 elif c == '\f':
32 rc = "'\\f'"
33 if not flags:
34 print(" 0, /* 0x%x %s */" % (i, rc))
35 else:
36 print(" %s, /* 0x%x %s */" % ("|".join(flags), i, rc))
38 for i in range(128, 256, 16):
39 print(" %s," % ", ".join(16*["0"]))
41 print("};")
42 print("")
44 for name in NAMES:
45 print("#define IS%s(c) (ctype_table[Py_CHARMASK(c)] & FLAG_%s)" %
46 (name, name))
48 print("")
50 for name in NAMES:
51 name = "is" + name.lower()
52 print("#undef %s" % name)
53 print("#define %s(c) undefined_%s(c)" % (name, name))
55 print("""
56 static unsigned char ctype_tolower[256] = {""")
58 for i in range(0, 256, 8):
59 values = []
60 for i in range(i, i+8):
61 if i < 128:
62 c = chr(i)
63 if c.isupper():
64 i = ord(c.lower())
65 values.append("0x%02x" % i)
66 print(" %s," % ", ".join(values))
68 print("};")
70 print("""
71 static unsigned char ctype_toupper[256] = {""")
73 for i in range(0, 256, 8):
74 values = []
75 for i in range(i, i+8):
76 if i < 128:
77 c = chr(i)
78 if c.islower():
79 i = ord(c.upper())
80 values.append("0x%02x" % i)
81 print(" %s," % ", ".join(values))
83 print("};")
85 print("""
86 #define TOLOWER(c) (ctype_tolower[Py_CHARMASK(c)])
87 #define TOUPPER(c) (ctype_toupper[Py_CHARMASK(c)])
89 #undef tolower
90 #define tolower(c) undefined_tolower(c)
91 #undef toupper
92 #define toupper(c) undefined_toupper(c)
93 """)