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