Lua 源码字符识别

主要包含在了 lctype.hlctype.c 中,代码量也很少。

//lctype.h
#if !defined(LUA_USE_CTYPE)
    #if 'A' == 65 && '0' == 48
        #define LUA_USE_CTYPE    0
    #else
        #define LUA_USE_CTYPE    1
    #endif
#endif

#if !LUA_USE_CTYPE    
    #include <limits.h>
    #include "llimits.h"
    #define ALPHABIT     0
    #define DIGITBIT     1
    #define PRINTBIT     2
    #define SPACEBIT     3
    #define XDIGITBIT    4
    #define MASK(B)          (1 << (B))
    #define testprop(c,p)    (luai_ctype_[(c)+1] & (p))
    #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))
    #define ltolower(c)      ((c) | ('A' ^ 'a'))
    LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2];
#else            
    #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

LUA_USE_CTYPE 意思是使用标准C的识别函数
否则将会用自定义的方法进行判断。
标准判断方法就不用说了, 就是使用 ctype.h 中的函数进行判断。
自定义判断主要依赖 luai_ctype_ 这个数组结构:

//lctype.c
#include <limits.h>
LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
  0x00,  /* EOZ */
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,	/* 0. */
  0x00,  0x08,  0x08,  0x08,  0x08,  0x08,  0x00,  0x00,
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,	/* 1. */
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
  0x0c,  0x04,  0x04,  0x04,  0x04,  0x04,  0x04,  0x04,	/* 2. */
  0x04,  0x04,  0x04,  0x04,  0x04,  0x04,  0x04,  0x04,
  0x16,  0x16,  0x16,  0x16,  0x16,  0x16,  0x16,  0x16,	/* 3. */
  0x16,  0x16,  0x04,  0x04,  0x04,  0x04,  0x04,  0x04,
  0x04,  0x15,  0x15,  0x15,  0x15,  0x15,  0x15,  0x05,	/* 4. */
  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,
  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,	/* 5. */
  0x05,  0x05,  0x05,  0x04,  0x04,  0x04,  0x04,  0x05,
  0x04,  0x15,  0x15,  0x15,  0x15,  0x15,  0x15,  0x05,	/* 6. */
  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,
  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,	/* 7. */
  0x05,  0x05,  0x05,  0x04,  0x04,  0x04,  0x04,  0x00,
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,	/* 8. */
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,	/* 9. */
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,	/* a. */
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,	/* b. */
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,	/* c. */
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,	/* d. */
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,	/* e. */
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,	/* f. */
  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
};

判断一个字符是否为字母就是判断在 luai_ctype_ 这个数组中的倒数第一位 (0000 0001) 是否为1。

lislalpha(c)     testprop(c, MASK(ALPHABIT))
//展开就是 
luai_ctype_[c+1] & 1 

例如,一个字符的编码是0x41,在 luai_ctype_ 中的存储的数值是 0x15(0001 0101)

这也就代表:

十六进制空白可打印数字字母
10101

0x2F 表示这个字符可以用来表示十六进制数值,可以打印,属于字母。
0x04 代表这个字符只可以打印
0x05 代表字符可打印,属于字母。
其余判断方法大同小异,剩下一个定义是进行小写转换的:

ltolower(c)      ((c) | ('A' ^ 'a'))

将字符转为小写,大小写字母主要区别在于第三位

'A' ^ 'a' == 0x20 (0010 0000)
c | 0x20 就可以将大写字母转换为小写。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值