~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Tools/scripts/make_ctype.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Script that generates the ctype.h-replacement in stringobject.c."""
 
2
 
 
3
NAMES = ("LOWER", "UPPER", "ALPHA", "DIGIT", "XDIGIT", "ALNUM", "SPACE")
 
4
 
 
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
 
13
 
 
14
static unsigned int ctype_table[256] = {""")
 
15
 
 
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))
 
37
 
 
38
for i in range(128, 256, 16):
 
39
    print("    %s," % ", ".join(16*["0"]))
 
40
 
 
41
print("};")
 
42
print("")
 
43
 
 
44
for name in NAMES:
 
45
    print("#define IS%s(c) (ctype_table[Py_CHARMASK(c)] & FLAG_%s)" %
 
46
          (name, name))
 
47
 
 
48
print("")
 
49
 
 
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))
 
54
 
 
55
print("""
 
56
static unsigned char ctype_tolower[256] = {""")
 
57
 
 
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))
 
67
 
 
68
print("};")
 
69
 
 
70
print("""
 
71
static unsigned char ctype_toupper[256] = {""")
 
72
 
 
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))
 
82
 
 
83
print("};")
 
84
 
 
85
print("""
 
86
#define TOLOWER(c) (ctype_tolower[Py_CHARMASK(c)])
 
87
#define TOUPPER(c) (ctype_toupper[Py_CHARMASK(c)])
 
88
 
 
89
#undef tolower
 
90
#define tolower(c) undefined_tolower(c)
 
91
#undef toupper
 
92
#define toupper(c) undefined_toupper(c)
 
93
""")