PageRenderTime 56ms CodeModel.GetById 23ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/python/scim/ascii.py

http://scim-python.googlecode.com/
Python | 115 lines | 95 code | 4 blank | 16 comment | 1 complexity | a9c12d17a1e0c1bd4d7cb866f442afe0 MD5 | raw file
  1# This file is copied verbatim from Python 2.5.1.
  2#
  3# Python 2.5.1 is:
  4#
  5# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software
  6# Foundation.  All rights reserved.
  7# Copyright (c) 2000 BeOpen.com.  All rights reserved.
  8# Copyright (c) 1995-2001 Corporation for National Research Initiatives.
  9# All rights reserved.
 10# Copyright (c) 1991-1995 Stichting Mathematisch Centrum.  All rights
 11# reserved.
 12#
 13# See the file "LICENSE.Python" for information on the history of
 14# Python, terms & conditions for usage, and a DISCLAIMER OF ALL
 15# WARRANTIES.
 16
 17"""Constants and membership tests for ASCII characters"""
 18
 19NUL     = 0x00  # ^@
 20SOH     = 0x01  # ^A
 21STX     = 0x02  # ^B
 22ETX     = 0x03  # ^C
 23EOT     = 0x04  # ^D
 24ENQ     = 0x05  # ^E
 25ACK     = 0x06  # ^F
 26BEL     = 0x07  # ^G
 27BS      = 0x08  # ^H
 28TAB     = 0x09  # ^I
 29HT      = 0x09  # ^I
 30LF      = 0x0a  # ^J
 31NL      = 0x0a  # ^J
 32VT      = 0x0b  # ^K
 33FF      = 0x0c  # ^L
 34CR      = 0x0d  # ^M
 35SO      = 0x0e  # ^N
 36SI      = 0x0f  # ^O
 37DLE     = 0x10  # ^P
 38DC1     = 0x11  # ^Q
 39DC2     = 0x12  # ^R
 40DC3     = 0x13  # ^S
 41DC4     = 0x14  # ^T
 42NAK     = 0x15  # ^U
 43SYN     = 0x16  # ^V
 44ETB     = 0x17  # ^W
 45CAN     = 0x18  # ^X
 46EM      = 0x19  # ^Y
 47SUB     = 0x1a  # ^Z
 48ESC     = 0x1b  # ^[
 49FS      = 0x1c  # ^\
 50GS      = 0x1d  # ^]
 51RS      = 0x1e  # ^^
 52US      = 0x1f  # ^_
 53SP      = 0x20  # space
 54DEL     = 0x7f  # delete
 55
 56controlnames = [
 57"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
 58"BS",  "HT",  "LF",  "VT",  "FF",  "CR",  "SO",  "SI",
 59"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
 60"CAN", "EM",  "SUB", "ESC", "FS",  "GS",  "RS",  "US",
 61"SP"
 62]
 63
 64def _ctoi(c):
 65    if type(c) == type(""):
 66        return ord(c)
 67    else:
 68        return c
 69
 70def isalnum(c): return isalpha(c) or isdigit(c)
 71def isalpha(c): return isupper(c) or islower(c)
 72def isascii(c): return _ctoi(c) <= 127          # ?
 73def isblank(c): return _ctoi(c) in (8,32)
 74def iscntrl(c): return _ctoi(c) <= 31
 75def isdigit(c): return _ctoi(c) >= 48 and _ctoi(c) <= 57
 76def isgraph(c): return _ctoi(c) >= 33 and _ctoi(c) <= 126
 77def islower(c): return _ctoi(c) >= 97 and _ctoi(c) <= 122
 78def isprint(c): return _ctoi(c) >= 32 and _ctoi(c) <= 126
 79def ispunct(c): return _ctoi(c) != 32 and not isalnum(c)
 80def isspace(c): return _ctoi(c) in (9, 10, 11, 12, 13, 32)
 81def isupper(c): return _ctoi(c) >= 65 and _ctoi(c) <= 90
 82def isxdigit(c): return isdigit(c) or \
 83    (_ctoi(c) >= 65 and _ctoi(c) <= 70) or (_ctoi(c) >= 97 and _ctoi(c) <= 102)
 84def isctrl(c): return _ctoi(c) < 32
 85def ismeta(c): return _ctoi(c) > 127
 86
 87def ascii(c):
 88    if type(c) == type(""):
 89        return chr(_ctoi(c) & 0x7f)
 90    else:
 91        return _ctoi(c) & 0x7f
 92
 93def ctrl(c):
 94    if type(c) == type(""):
 95        return chr(_ctoi(c) & 0x1f)
 96    else:
 97        return _ctoi(c) & 0x1f
 98
 99def alt(c):
100    if type(c) == type(""):
101        return chr(_ctoi(c) | 0x80)
102    else:
103        return _ctoi(c) | 0x80
104
105def unctrl(c):
106    bits = _ctoi(c)
107    if bits == 0x7f:
108        rep = "^?"
109    elif isprint(bits & 0x7f):
110        rep = chr(bits & 0x7f)
111    else:
112        rep = "^" + chr(((bits & 0x7f) | 0x20) + 0x20)
113    if bits & 0x80:
114        return "!" + rep
115    return rep