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