http://www.cplusplus.com/reference/cctype/
链接为cpp reference 中的ctype 定义,参考这里来写的代码。
首先用这个代码段,生产一个TYPE_FLAG的数组。
// Generate Type Flag array
void gen_flag(void){
unsigned short flag_type[128] = {};
int i;
// 0x00 ~ 0x08
for(i=0; i<=8; i++)
flag_type[i] |= FLAG_CONTROL;
// 0x09
flag_type[0x09] |= FLAG_CONTROL | FLAG_BLANK | FLAG_SPACE;
// 0x0a ~ 0x0d
for(i=0x0a; i<=0x0d; i++)
flag_type[i] |= FLAG_CONTROL | FLAG_SPACE;
// 0x0e ~ 0x1f
for(i=0x0e; i<= 0x1f; i++)
flag_type[i] |= FLAG_CONTROL;
// 0x20
flag_type[0x20] |= FLAG_BLANK | FLAG_SPACE;
// 0x21 ~ 0x2f
for(i=0x21; i<= 0x2f; i++)
flag_type[i] |= FLAG_PUNCT;
// 0x30 ~ 0x39
for(i=0x30; i<= 0x39; i++)
flag_type[i] |= FLAG_DIGIT | FLAG_HEX;
// 0x3A ~ 0x40
for(i=0x3A; i<= 0x40; i++)
flag_type[i] |= FLAG_PUNCT;
// 0x41 ~ 0x46
for(i=0x41; i<= 0x46; i++)
flag_type[i] |= FLAG_UPPER | FLAG_ALPHA | FLAG_HEX;
// 0x47 ~ 0x5A
for(i=0x47; i<= 0x5A; i++)
flag_type[i] |= FLAG_UPPER | FLAG_ALPHA;
// 0x5B ~ 0x60
for(i=0x5b; i<= 0x60; i++)
flag_type[i] |= FLAG_PUNCT;
// 0x61 ~ 0x66
for(i=0x61; i<= 0x66; i++)
flag_type[i] |= FLAG_LOWER | FLAG_ALPHA | FLAG_HEX;
// 0x67 ~ 0x7A
for(i=0x67; i<= 0x7A; i++)
flag_type[i] |= FLAG_ALPHA | FLAG_LOWER;
// 0x7b ~ 0x7e
for(i=0x7b; i<= 0x7f; i++)
flag_type[i] |= FLAG_PUNCT;
flag_type[0x7f] = FLAG_CONTROL;
for(i=0;i<=0x7f; i++){
printf("0x%04x",(int)flag_type[i]);
if(7 == (i%8)){
printf(",\n");
}else{
printf(", ");
}
}
}
test.cpp
#include <iostream>
#include<cstdio>
#include "istype.h"
using namespace std;
int main(int agrc, char* argv[]){
char test[] = {0x0, 'a', '9', ',', '\\'};
for(int i=0; i< sizeof(test)/sizeof(char); i++){
if (is_alpha(test[i])){
cout << hex << test[i] << " = ";
cout.put(test[i]);
cout << " is alpha.\n";
}else if (is_digit(test[i])){
cout << hex << test[i] << " = ";
cout.put(test[i]);
cout << " is digit.\n";
}else if (is_punct(test[i])){
cout << hex << test[i] << " = ";
cout.put(test[i]);
cout << " is punctuation.\n";
}else if (is_cntrl(test[i])){
cout << hex << test[i] << " = ";
cout.put(test[i]);
cout << " is control.\n";
}
}
return 0;
}
istype.h
/*
* Copyright (c) 2015. 美细耐斯(上海)电子有限公司
* All rights reserved.
* @author heafei@aliyun.com
* @date 20151231
*/
#ifndef ISTYPE_H
#define ISTYPE_H
bool is_alpha(int ch);
bool is_upper(int ch);
bool is_lower(int ch);
bool is_digit(int ch);
bool is_xdigit(int ch);
bool is_cntrl(int ch);
bool is_punct(int ch);
bool is_print(int ch);
bool is_graph(int ch);
char to_upper(int ch);
char to_lower(int ch);
#endif // ISTYPE_H
// istype.cpp
/*
* Copyright (c) 2015.
* All rights reserved.
* @author heafei@aliyun.com
* @date 20151231
*/
#include <cstdio>
#include "istype.h"
using namespace std;
enum TypeFlag{
FLAG_UPPER = 0x0001,
FLAG_LOWER = 0x0002,
FLAG_ALPHA = 0x0103,
FLAG_DIGIT = 0x0004,
FLAG_SPACE = 0x0008, /* HT LF VT FF CR SP */
FLAG_PUNCT = 0x0010,
FLAG_CONTROL = 0x0020,
FLAG_BLANK = 0x0040, /* this is SP only, not SP and HT as in C99 */
FLAG_HEX = 0x0080,
FLAG_LEADBYTE = 0x8000,
};
// ASCII -- American Standard Code for Information Interchange
// TYPE_FLAG_ARRY[0] is flag of 0x0 in ASCII
const unsigned short TYPE_FLAG_ARRY[128] = {
0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
0x0020, 0x0068, 0x0028, 0x0028, 0x0028, 0x0028, 0x0020, 0x0020,
0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
0x0048, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084,
0x0084, 0x0084, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
0x0010, 0x0183, 0x0183, 0x0183, 0x0183, 0x0183, 0x0183, 0x0103,
0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103,
0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103,
0x0103, 0x0103, 0x0103, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
0x0010, 0x0183, 0x0183, 0x0183, 0x0183, 0x0183, 0x0183, 0x0103,
0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103,
0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103, 0x0103,
0x0103, 0x0103, 0x0103, 0x0010, 0x0010, 0x0010, 0x0010, 0x0020,
};
bool is_lower(int ch){
return TYPE_FLAG_ARRY[ch]&FLAG_LOWER;
}
bool is_upper(int ch){
return TYPE_FLAG_ARRY[ch]&FLAG_UPPER;
}
bool is_alpha(int ch){
return TYPE_FLAG_ARRY[ch]&FLAG_ALPHA;
}
bool is_digit(int ch){
return TYPE_FLAG_ARRY[ch] & FLAG_DIGIT;
}
bool is_xdigit(int ch){
return TYPE_FLAG_ARRY[ch] & FLAG_HEX;
}
bool is_alnum(int ch){
return TYPE_FLAG_ARRY[ch] & (FLAG_DIGIT | FLAG_ALPHA);
}
bool is_blank(int ch){
return TYPE_FLAG_ARRY[ch] & FLAG_BLANK;
}
bool is_cntrl(int ch){
return TYPE_FLAG_ARRY[ch] & FLAG_CONTROL;
}
bool is_punct(int ch){
return TYPE_FLAG_ARRY[ch] & FLAG_PUNCT;
}
bool is_print(int ch){
return (ch <= 0x7e) && (ch >= 0x20);
}
bool is_graph(int ch){
return (ch <= 0x7e) && (ch >= 0x21);
}
char to_upper(int ch){
#if defined(DEBUG_OLIVER)
printf("[%s] %c upper -> %c", __func__, ch, (is_alpha(ch) ? (ch-32) : ch));
#endif // defined(DEBUG_OLIVER)
return is_alpha(ch) ? (ch-32) : ch;
}
char to_lower(int ch){
#if defined(DEBUG_OLIVER)
printf("[%s] %c upper -> %c", __func__, ch, (is_alpha(ch) ? (ch+32) : ch));
#endif // defined(DEBUG_OLIVER)
return is_alpha(ch) ? (ch+32) : ch;
}