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