Lua源码学习-ltablib.h

/*
** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $
** 'ctype' functions for Lua
** See Copyright Notice in lua.h
*/

#ifndef lctype_h
#define lctype_h

#include "lua.h"


/*
** WARNING: the functions defined here do not necessarily correspond
** to the similar functions in the standard C ctype.h. They are
** optimized for the specific needs of Lua
*/

#if !defined(LUA_USE_CTYPE)

#if 'A' == 65 && '0' == 48
/* ASCII case: can use its own tables; faster and fixed */
#define LUA_USE_CTYPE    0
#else
/* must use standard C ctype */
#define LUA_USE_CTYPE    1
#endif

#endif


#if !LUA_USE_CTYPE    /* { */

#include <limits.h>

#include "llimits.h"


#define ALPHABIT    0        /* ['A-Z','a-z'], */
#define DIGITBIT    1        /* ['0-9'], */
#define PRINTBIT    2
#define SPACEBIT    3
#define XDIGITBIT    4        /* ['0-9','A-F','a-f'],


#define MASK(B)        (1 << (B))


/*
** add 1 to char to allow index -1 (EOZ)
*/
#define testprop(c,p)    (luai_ctype_[(c)+1] & (p))

/*
** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_'
*/
#define lislalpha(c)    testprop(c, MASK(ALPHABIT))
#define lislalnum(c)    testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT)))
#define lisdigit(c)    testprop(c, MASK(DIGITBIT))
#define lisspace(c)    testprop(c, MASK(SPACEBIT))
#define lisprint(c)    testprop(c, MASK(PRINTBIT))
#define lisxdigit(c)    testprop(c, MASK(XDIGITBIT))

/*
** this 'ltolower' only works for alphabetic characters
** 
** 仔细看下ASCII表中的A,a的bits值,
** A.bits == 0100 0001    a.bits == 0110 0001
** ret = A^a == 0010 0000, c|ret---->将c转小写,作者暗示编码表中大小写有一定的对照关系,且能用A^a表示出来,亦即:假设了编码表是按照某种规则...
*/
#define ltolower(c)    ((c) | ('A' ^ 'a'))


/* two more entries for 0 and -1 (EOZ) */
LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2];


#else            /* }{ */

/*
** use standard C ctypes
*/

#include <ctype.h>


#define lislalpha(c)    (isalpha(c) || (c) == '_')
#define lislalnum(c)    (isalnum(c) || (c) == '_')
#define lisdigit(c)    (isdigit(c))
#define lisspace(c)    (isspace(c))
#define lisprint(c)    (isprint(c))
#define lisxdigit(c)    (isxdigit(c))

#define ltolower(c)    (tolower(c))

#endif            /* } */

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值