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