(root)/
Python-3.11.7/
Tools/
scripts/
make_ctype.py
       1  #!/usr/bin/env python3
       2  """Script that generates the ctype.h-replacement in stringobject.c."""
       3  
       4  NAMES = ("LOWER", "UPPER", "ALPHA", "DIGIT", "XDIGIT", "ALNUM", "SPACE")
       5  
       6  print("""
       7  #define FLAG_LOWER  0x01
       8  #define FLAG_UPPER  0x02
       9  #define FLAG_ALPHA  (FLAG_LOWER|FLAG_UPPER)
      10  #define FLAG_DIGIT  0x04
      11  #define FLAG_ALNUM  (FLAG_ALPHA|FLAG_DIGIT)
      12  #define FLAG_SPACE  0x08
      13  #define FLAG_XDIGIT 0x10
      14  
      15  static unsigned int ctype_table[256] = {""")
      16  
      17  for i in range(128):
      18      c = chr(i)
      19      flags = []
      20      for name in NAMES:
      21          if name in ("ALPHA", "ALNUM"):
      22              continue
      23          if name == "XDIGIT":
      24              method = lambda: c.isdigit() or c.upper() in "ABCDEF"
      25          else:
      26              method = getattr(c, "is" + name.lower())
      27          if method():
      28              flags.append("FLAG_" + name)
      29      rc = repr(c)
      30      if c == '\v':
      31          rc = "'\\v'"
      32      elif c == '\f':
      33          rc = "'\\f'"
      34      if not flags:
      35          print("    0, /* 0x%x %s */" % (i, rc))
      36      else:
      37          print("    %s, /* 0x%x %s */" % ("|".join(flags), i, rc))
      38  
      39  for i in range(128, 256, 16):
      40      print("    %s," % ", ".join(16*["0"]))
      41  
      42  print("};")
      43  print("")
      44  
      45  for name in NAMES:
      46      print("#define IS%s(c) (ctype_table[Py_CHARMASK(c)] & FLAG_%s)" %
      47            (name, name))
      48  
      49  print("")
      50  
      51  for name in NAMES:
      52      name = "is" + name.lower()
      53      print("#undef %s" % name)
      54      print("#define %s(c) undefined_%s(c)" % (name, name))
      55  
      56  print("""
      57  static unsigned char ctype_tolower[256] = {""")
      58  
      59  for i in range(0, 256, 8):
      60      values = []
      61      for i in range(i, i+8):
      62          if i < 128:
      63              c = chr(i)
      64              if c.isupper():
      65                  i = ord(c.lower())
      66          values.append("0x%02x" % i)
      67      print("    %s," % ", ".join(values))
      68  
      69  print("};")
      70  
      71  print("""
      72  static unsigned char ctype_toupper[256] = {""")
      73  
      74  for i in range(0, 256, 8):
      75      values = []
      76      for i in range(i, i+8):
      77          if i < 128:
      78              c = chr(i)
      79              if c.islower():
      80                  i = ord(c.upper())
      81          values.append("0x%02x" % i)
      82      print("    %s," % ", ".join(values))
      83  
      84  print("};")
      85  
      86  print("""
      87  #define TOLOWER(c) (ctype_tolower[Py_CHARMASK(c)])
      88  #define TOUPPER(c) (ctype_toupper[Py_CHARMASK(c)])
      89  
      90  #undef tolower
      91  #define tolower(c) undefined_tolower(c)
      92  #undef toupper
      93  #define toupper(c) undefined_toupper(c)
      94  """)