lctype.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. ** $Id: lctype.h $
  3. ** 'ctype' functions for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef lctype_h
  7. #define lctype_h
  8. #include "lua.h"
  9. /*
  10. ** WARNING: the functions defined here do not necessarily correspond
  11. ** to the similar functions in the standard C ctype.h. They are
  12. ** optimized for the specific needs of Lua.
  13. */
  14. #if !defined(LUA_USE_CTYPE)
  15. #if 'A' == 65 && '0' == 48
  16. /* ASCII case: can use its own tables; faster and fixed */
  17. #define LUA_USE_CTYPE 0
  18. #else
  19. /* must use standard C ctype */
  20. #define LUA_USE_CTYPE 1
  21. #endif
  22. #endif
  23. #if !LUA_USE_CTYPE /* { */
  24. #include <limits.h>
  25. #include "llimits.h"
  26. #define ALPHABIT 0
  27. #define DIGITBIT 1
  28. #define PRINTBIT 2
  29. #define SPACEBIT 3
  30. #define XDIGITBIT 4
  31. #define MASK(B) (1 << (B))
  32. /*
  33. ** add 1 to char to allow index -1 (EOZ)
  34. */
  35. #define testprop(c,p) (luai_ctype_[(c)+1] & (p))
  36. /*
  37. ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_'
  38. */
  39. #define lislalpha(c) testprop(c, MASK(ALPHABIT))
  40. #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT)))
  41. #define lisdigit(c) testprop(c, MASK(DIGITBIT))
  42. #define lisspace(c) testprop(c, MASK(SPACEBIT))
  43. #define lisprint(c) testprop(c, MASK(PRINTBIT))
  44. #define lisxdigit(c) testprop(c, MASK(XDIGITBIT))
  45. /*
  46. ** In ASCII, this 'ltolower' is correct for alphabetic characters and
  47. ** for '.'. That is enough for Lua needs. ('check_exp' ensures that
  48. ** the character either is an upper-case letter or is unchanged by
  49. ** the transformation, which holds for lower-case letters and '.'.)
  50. */
  51. #define ltolower(c) \
  52. check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \
  53. (c) | ('A' ^ 'a'))
  54. /* one entry for each character and for -1 (EOZ) */
  55. LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];)
  56. #else /* }{ */
  57. /*
  58. ** use standard C ctypes
  59. */
  60. #include <ctype.h>
  61. #define lislalpha(c) (isalpha(c) || (c) == '_')
  62. #define lislalnum(c) (isalnum(c) || (c) == '_')
  63. #define lisdigit(c) (isdigit(c))
  64. #define lisspace(c) (isspace(c))
  65. #define lisprint(c) (isprint(c))
  66. #define lisxdigit(c) (isxdigit(c))
  67. #define ltolower(c) (tolower(c))
  68. #endif /* } */
  69. #endif