lctype.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. ** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $
  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. ** this 'ltolower' only works for alphabetic characters
  47. */
  48. #define ltolower(c) ((c) | ('A' ^ 'a'))
  49. /* two more entries for 0 and -1 (EOZ) */
  50. LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2];
  51. #else /* }{ */
  52. /*
  53. ** use standard C ctypes
  54. */
  55. #include <ctype.h>
  56. #define lislalpha(c) (isalpha(c) || (c) == '_')
  57. #define lislalnum(c) (isalnum(c) || (c) == '_')
  58. #define lisdigit(c) (isdigit(c))
  59. #define lisspace(c) (isspace(c))
  60. #define lisprint(c) (isprint(c))
  61. #define lisxdigit(c) (isxdigit(c))
  62. #define ltolower(c) (tolower(c))
  63. #endif /* } */
  64. #endif