• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Constants and membership tests for ASCII characters"""
2
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 0 <= _ctoi(c) <= 127          # ?
57def isblank(c): return _ctoi(c) in (9, 32)
58def iscntrl(c): return 0 <= _ctoi(c) <= 31 or _ctoi(c) == 127
59def isdigit(c): return 48 <= _ctoi(c) <= 57
60def isgraph(c): return 33 <= _ctoi(c) <= 126
61def islower(c): return 97 <= _ctoi(c) <= 122
62def isprint(c): return 32 <= _ctoi(c) <= 126
63def ispunct(c): return isgraph(c) and not isalnum(c)
64def isspace(c): return _ctoi(c) in (9, 10, 11, 12, 13, 32)
65def isupper(c): return 65 <= _ctoi(c) <= 90
66def isxdigit(c): return isdigit(c) or \
67    (65 <= _ctoi(c) <= 70) or (97 <= _ctoi(c) <= 102)
68def isctrl(c): return 0 <= _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 isprint(bits & 0x7f):
94        rep = chr(bits & 0x7f)
95    else:
96        rep = "^" + chr(((bits & 0x7f) | 0x20) + 0x20)
97    if bits & 0x80:
98        return "!" + rep
99    return rep
100