xref: /freebsd/sys/contrib/openzfs/module/lua/lctype.h (revision 1f474190)
1 /* BEGIN CSTYLED */
2 /*
3 ** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $
4 ** 'ctype' functions for Lua
5 ** See Copyright Notice in lua.h
6 */
7 
8 #ifndef lctype_h
9 #define lctype_h
10 
11 #include <sys/lua/lua.h>
12 
13 
14 /*
15 ** WARNING: the functions defined here do not necessarily correspond
16 ** to the similar functions in the standard C ctype.h. They are
17 ** optimized for the specific needs of Lua
18 */
19 
20 #if !defined(LUA_USE_CTYPE)
21 
22 #if 'A' == 65 && '0' == 48
23 /* ASCII case: can use its own tables; faster and fixed */
24 #define LUA_USE_CTYPE	0
25 #else
26 /* must use standard C ctype */
27 #define LUA_USE_CTYPE	1
28 #endif
29 
30 #endif
31 
32 
33 #if !LUA_USE_CTYPE	/* { */
34 
35 #include "llimits.h"
36 
37 
38 #define ALPHABIT	0
39 #define DIGITBIT	1
40 #define PRINTBIT	2
41 #define SPACEBIT	3
42 #define XDIGITBIT	4
43 
44 
45 #define MASK(B)		(1 << (B))
46 
47 
48 /*
49 ** add 1 to char to allow index -1 (EOZ)
50 */
51 #define testprop(c,p)	(luai_ctype_[(lu_byte)(c)+1] & (p))
52 
53 /*
54 ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_'
55 */
56 #define lislalpha(c)	testprop(c, MASK(ALPHABIT))
57 #define lislalnum(c)	testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT)))
58 #define lisdigit(c)	testprop(c, MASK(DIGITBIT))
59 #define lisspace(c)	testprop(c, MASK(SPACEBIT))
60 #define lisprint(c)	testprop(c, MASK(PRINTBIT))
61 #define lisxdigit(c)	testprop(c, MASK(XDIGITBIT))
62 
63 /*
64 ** this 'ltolower' only works for alphabetic characters
65 */
66 #define ltolower(c)	((c) | ('A' ^ 'a'))
67 
68 
69 /* two more entries for 0 and -1 (EOZ) */
70 LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2];
71 
72 
73 #else			/* }{ */
74 
75 /*
76 ** use standard C ctypes
77 */
78 
79 #include <ctype.h>
80 
81 
82 #define lislalpha(c)	(isalpha(c) || (c) == '_')
83 #define lislalnum(c)	(isalnum(c) || (c) == '_')
84 #define lisdigit(c)	(isdigit(c))
85 #define lisspace(c)	(isspace(c))
86 #define lisprint(c)	(isprint(c))
87 #define lisxdigit(c)	(isxdigit(c))
88 
89 #define ltolower(c)	(tolower(c))
90 
91 #endif			/* } */
92 
93 #endif
94 /* END CSTYLED */
95