gen_util.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # common utility functions for all bindings generators
  2. import re
  3. re_1d_array = re.compile(r"^(?:const )?\w*\s*\*?\[\d*\]$")
  4. re_2d_array = re.compile(r"^(?:const )?\w*\s*\*?\[\d*\]\[\d*\]$")
  5. def is_1d_array_type(s):
  6. return re_1d_array.match(s) is not None
  7. def is_2d_array_type(s):
  8. return re_2d_array.match(s) is not None
  9. def is_array_type(s):
  10. return is_1d_array_type(s) or is_2d_array_type(s)
  11. def extract_array_type(s):
  12. return s[:s.index('[')].strip()
  13. def extract_array_sizes(s):
  14. return s[s.index('['):].replace('[', ' ').replace(']', ' ').split()
  15. def is_string_ptr(s):
  16. return s == "const char *"
  17. def is_const_void_ptr(s):
  18. return s == "const void *"
  19. def is_void_ptr(s):
  20. return s == "void *"
  21. def is_func_ptr(s):
  22. return '(*)' in s
  23. def extract_ptr_type(s):
  24. tokens = s.split()
  25. if tokens[0] == 'const':
  26. return tokens[1]
  27. else:
  28. return tokens[0]
  29. # PREFIX_BLA_BLUB to bla_blub
  30. def as_lower_snake_case(s, prefix):
  31. outp = s.lower()
  32. if outp.startswith(prefix):
  33. outp = outp[len(prefix):]
  34. return outp
  35. # prefix_bla_blub => blaBlub, PREFIX_BLA_BLUB => blaBlub
  36. def as_lower_camel_case(s, prefix):
  37. outp = s.lower()
  38. if outp.startswith(prefix):
  39. outp = outp[len(prefix):]
  40. parts = outp.split('_')
  41. outp = parts[0]
  42. for part in parts[1:]:
  43. outp += part.capitalize()
  44. return outp