- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathascii.py
99 lines (91 loc) · 2.48 KB
/
ascii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Constants and membership tests for ASCII characters"""
NUL=0x00# ^@
SOH=0x01# ^A
STX=0x02# ^B
ETX=0x03# ^C
EOT=0x04# ^D
ENQ=0x05# ^E
ACK=0x06# ^F
BEL=0x07# ^G
BS=0x08# ^H
TAB=0x09# ^I
HT=0x09# ^I
LF=0x0a# ^J
NL=0x0a# ^J
VT=0x0b# ^K
FF=0x0c# ^L
CR=0x0d# ^M
SO=0x0e# ^N
SI=0x0f# ^O
DLE=0x10# ^P
DC1=0x11# ^Q
DC2=0x12# ^R
DC3=0x13# ^S
DC4=0x14# ^T
NAK=0x15# ^U
SYN=0x16# ^V
ETB=0x17# ^W
CAN=0x18# ^X
EM=0x19# ^Y
SUB=0x1a# ^Z
ESC=0x1b# ^[
FS=0x1c# ^\
GS=0x1d# ^]
RS=0x1e# ^^
US=0x1f# ^_
SP=0x20# space
DEL=0x7f# delete
controlnames= [
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
"SP"
]
def_ctoi(c):
ifisinstance(c, str):
returnord(c)
else:
returnc
defisalnum(c): returnisalpha(c) orisdigit(c)
defisalpha(c): returnisupper(c) orislower(c)
defisascii(c): return0<=_ctoi(c) <=127# ?
defisblank(c): return_ctoi(c) in (9, 32)
defiscntrl(c): return0<=_ctoi(c) <=31or_ctoi(c) ==127
defisdigit(c): return48<=_ctoi(c) <=57
defisgraph(c): return33<=_ctoi(c) <=126
defislower(c): return97<=_ctoi(c) <=122
defisprint(c): return32<=_ctoi(c) <=126
defispunct(c): returnisgraph(c) andnotisalnum(c)
defisspace(c): return_ctoi(c) in (9, 10, 11, 12, 13, 32)
defisupper(c): return65<=_ctoi(c) <=90
defisxdigit(c): returnisdigit(c) or \
(65<=_ctoi(c) <=70) or (97<=_ctoi(c) <=102)
defisctrl(c): return0<=_ctoi(c) <32
defismeta(c): return_ctoi(c) >127
defascii(c):
ifisinstance(c, str):
returnchr(_ctoi(c) &0x7f)
else:
return_ctoi(c) &0x7f
defctrl(c):
ifisinstance(c, str):
returnchr(_ctoi(c) &0x1f)
else:
return_ctoi(c) &0x1f
defalt(c):
ifisinstance(c, str):
returnchr(_ctoi(c) |0x80)
else:
return_ctoi(c) |0x80
defunctrl(c):
bits=_ctoi(c)
ifbits==0x7f:
rep="^?"
elifisprint(bits&0x7f):
rep=chr(bits&0x7f)
else:
rep="^"+chr(((bits&0x7f) |0x20) +0x20)
ifbits&0x80:
return"!"+rep
returnrep