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