Linux内核中isdigit的实现

在标准C中,isdigit函数可以用来判断字符是否为0~9之间的数字。比如:

int a = isdigit('1');

int b = isdigit('a');
int c = isdigit(3);
可以使用宏定义去实现这个简单的函数,如下所示:

#define isdigit(c) ((c) >= '0' && (c) <= '9')
Linux内核中isdigit的实现,其代码如下所示:

#define _U 0x01 /* upper */

#define _L 0x02 /* lower */
#define _D 0x04 /* digit */
#define _C 0x08 /* cntrl */
#define _P 0x10 /* punct */
#define _S 0x20 /* white space (space/lf/tab) */
#define _X 0x40 /* hex digit */
#define _SP 0x80 /* hard space (0x20) */
 
extern unsigned char _ctype[];

#define isdigit(c) ((_ctype+1)[c]&(_D))

unsigned char _ctype[] = {0x00, /* EOF */
 _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */
 _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */
 _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */
 _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */
 _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */
 _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */
 _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */
 _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */
 _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */
 _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */
 _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */
 _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */
 _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */
 _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */
 _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */
 _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 160-175 */
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 176-191 */
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 192-207 */
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 208-223 */
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 224-239 */
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /* 240-255 */

字符'0'~'9'对应的ASCII码为48~57,映射到上面的_ctype数组,相应的位置全是_D,_D&_D则为真,其它的字符则判断为false。对不同种类的字符进行了分类,并使用唯一的二进制来进行标识,使用&和|保证了不同类别的字符不会同时满足两种分类的条件。

猜你喜欢

转载自www.linuxidc.com/Linux/2016-03/128864.htm