1 | n/a | """Constants and membership tests for ASCII characters""" |
---|
2 | n/a | |
---|
3 | n/a | NUL = 0x00 # ^@ |
---|
4 | n/a | SOH = 0x01 # ^A |
---|
5 | n/a | STX = 0x02 # ^B |
---|
6 | n/a | ETX = 0x03 # ^C |
---|
7 | n/a | EOT = 0x04 # ^D |
---|
8 | n/a | ENQ = 0x05 # ^E |
---|
9 | n/a | ACK = 0x06 # ^F |
---|
10 | n/a | BEL = 0x07 # ^G |
---|
11 | n/a | BS = 0x08 # ^H |
---|
12 | n/a | TAB = 0x09 # ^I |
---|
13 | n/a | HT = 0x09 # ^I |
---|
14 | n/a | LF = 0x0a # ^J |
---|
15 | n/a | NL = 0x0a # ^J |
---|
16 | n/a | VT = 0x0b # ^K |
---|
17 | n/a | FF = 0x0c # ^L |
---|
18 | n/a | CR = 0x0d # ^M |
---|
19 | n/a | SO = 0x0e # ^N |
---|
20 | n/a | SI = 0x0f # ^O |
---|
21 | n/a | DLE = 0x10 # ^P |
---|
22 | n/a | DC1 = 0x11 # ^Q |
---|
23 | n/a | DC2 = 0x12 # ^R |
---|
24 | n/a | DC3 = 0x13 # ^S |
---|
25 | n/a | DC4 = 0x14 # ^T |
---|
26 | n/a | NAK = 0x15 # ^U |
---|
27 | n/a | SYN = 0x16 # ^V |
---|
28 | n/a | ETB = 0x17 # ^W |
---|
29 | n/a | CAN = 0x18 # ^X |
---|
30 | n/a | EM = 0x19 # ^Y |
---|
31 | n/a | SUB = 0x1a # ^Z |
---|
32 | n/a | ESC = 0x1b # ^[ |
---|
33 | n/a | FS = 0x1c # ^\ |
---|
34 | n/a | GS = 0x1d # ^] |
---|
35 | n/a | RS = 0x1e # ^^ |
---|
36 | n/a | US = 0x1f # ^_ |
---|
37 | n/a | SP = 0x20 # space |
---|
38 | n/a | DEL = 0x7f # delete |
---|
39 | n/a | |
---|
40 | n/a | controlnames = [ |
---|
41 | n/a | "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", |
---|
42 | n/a | "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", |
---|
43 | n/a | "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", |
---|
44 | n/a | "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US", |
---|
45 | n/a | "SP" |
---|
46 | n/a | ] |
---|
47 | n/a | |
---|
48 | n/a | def _ctoi(c): |
---|
49 | n/a | if type(c) == type(""): |
---|
50 | n/a | return ord(c) |
---|
51 | n/a | else: |
---|
52 | n/a | return c |
---|
53 | n/a | |
---|
54 | n/a | def isalnum(c): return isalpha(c) or isdigit(c) |
---|
55 | n/a | def isalpha(c): return isupper(c) or islower(c) |
---|
56 | n/a | def isascii(c): return 0 <= _ctoi(c) <= 127 # ? |
---|
57 | n/a | def isblank(c): return _ctoi(c) in (9, 32) |
---|
58 | n/a | def iscntrl(c): return 0 <= _ctoi(c) <= 31 or _ctoi(c) == 127 |
---|
59 | n/a | def isdigit(c): return 48 <= _ctoi(c) <= 57 |
---|
60 | n/a | def isgraph(c): return 33 <= _ctoi(c) <= 126 |
---|
61 | n/a | def islower(c): return 97 <= _ctoi(c) <= 122 |
---|
62 | n/a | def isprint(c): return 32 <= _ctoi(c) <= 126 |
---|
63 | n/a | def ispunct(c): return isgraph(c) and not isalnum(c) |
---|
64 | n/a | def isspace(c): return _ctoi(c) in (9, 10, 11, 12, 13, 32) |
---|
65 | n/a | def isupper(c): return 65 <= _ctoi(c) <= 90 |
---|
66 | n/a | def isxdigit(c): return isdigit(c) or \ |
---|
67 | n/a | (65 <= _ctoi(c) <= 70) or (97 <= _ctoi(c) <= 102) |
---|
68 | n/a | def isctrl(c): return 0 <= _ctoi(c) < 32 |
---|
69 | n/a | def ismeta(c): return _ctoi(c) > 127 |
---|
70 | n/a | |
---|
71 | n/a | def ascii(c): |
---|
72 | n/a | if type(c) == type(""): |
---|
73 | n/a | return chr(_ctoi(c) & 0x7f) |
---|
74 | n/a | else: |
---|
75 | n/a | return _ctoi(c) & 0x7f |
---|
76 | n/a | |
---|
77 | n/a | def ctrl(c): |
---|
78 | n/a | if type(c) == type(""): |
---|
79 | n/a | return chr(_ctoi(c) & 0x1f) |
---|
80 | n/a | else: |
---|
81 | n/a | return _ctoi(c) & 0x1f |
---|
82 | n/a | |
---|
83 | n/a | def alt(c): |
---|
84 | n/a | if type(c) == type(""): |
---|
85 | n/a | return chr(_ctoi(c) | 0x80) |
---|
86 | n/a | else: |
---|
87 | n/a | return _ctoi(c) | 0x80 |
---|
88 | n/a | |
---|
89 | n/a | def unctrl(c): |
---|
90 | n/a | bits = _ctoi(c) |
---|
91 | n/a | if bits == 0x7f: |
---|
92 | n/a | rep = "^?" |
---|
93 | n/a | elif isprint(bits & 0x7f): |
---|
94 | n/a | rep = chr(bits & 0x7f) |
---|
95 | n/a | else: |
---|
96 | n/a | rep = "^" + chr(((bits & 0x7f) | 0x20) + 0x20) |
---|
97 | n/a | if bits & 0x80: |
---|
98 | n/a | return "!" + rep |
---|
99 | n/a | return rep |
---|