/python/scim/ascii.py

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