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