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