一天一个CRT函数 __toascii tolower toupper

来源:互联网 发布:tripmode windows破解 编辑:程序博客网 时间:2024/05/03 07:56

今天练习DataConversion剩余函数,涉及到abs、isascii、isspace、isdigit、isxdigit、iscntrl、isgraph、isprint、isalpha、isupper、islower、ispunct和toascii、tolower、toupper。厄,有点多,但是都很短小!也能从函数名知道该函数的作用(“顾名思义”)。

1.实现

   1: template<typename T>
   2: inline T tAbs(T tNum)
   3: {
   4:     return( tNum >= 0 ? tNum : -tNum );
   5: }
   6:  
   7: template<typename T>
   8: inline bool tIsAscii(T arg)
   9: {
  10:     return static_cast<unsigned int>(arg) < 0x80;    // Decimal 128, Bin 1000 0000
  11: }
  12:  
  13: template<typename T>
  14: inline bool tIsspace(T c)
  15: {
  16:     if( c == _T(' ')  ||
  17:         c == _T('/f') ||
  18:         c == _T('/n') ||
  19:         c == _T('/r') ||
  20:         c == _T('/t') ||
  21:         c == _T('/v') )
  22:         return true;
  23:     return false;
  24: }
  25:  
  26: template<typename T>
  27: inline bool tIsdigit(T c)
  28: {
  29:     if( c >= _T('0') && c <= _T('9') )
  30:         return true;
  31:     return false;
  32: }
  33:  
  34: template<typename T>
  35: inline bool tIsxdigit(T c)
  36: {
  37:     if( ( c >= _T('0') && c <= _T('9')) ||
  38:         ( c >= _T('a') && c <= _T('f')) ||
  39:         ( c >= _T('A') && c <= _T('F')) )
  40:         return true;
  41:     return false;
  42: }
  43:  
  44: template<typename T>
  45: inline bool tIscntrl(T c)
  46: {
  47:     if( c <= 0x1F || c == 0x7F )    // 0x1F==>31 0x7F==>127
  48:         return true;
  49:     return false;
  50: }
  51:  
  52: template<typename T>
  53: inline bool tIsgraph(T c)
  54: {
  55:     if ( c >= 0x21 && c <= 0x7E )    // 0x21==>33 0x7E==>126
  56:         return true;
  57:     return false;
  58: }
  59:  
  60: template<typename T>
  61: inline bool tIsprint(T c)
  62: {
  63:     if( c >= 0x20 && c <= 0x7E )    // 0x20==>32 0x7E==>126
  64:         return true;
  65:     return false;
  66: }
  67:  
  68: template<typename T>
  69: inline bool tIsalpha(T c)
  70: {
  71:     return ((c | 0x20) - _T('a')) < 26u; // 0x20==>32
  72: }
  73:  
  74: template<typename T>
  75: inline bool tIsupper(T c)
  76: {
  77:     if( c >= _T('A') && c <= _T('Z') )
  78:         return true;
  79:     return false;
  80: }
  81:  
  82: template<typename T>
  83: inline bool tIslower(T c)
  84: {
  85:     if( c >= _T('a') && c <= _T('z') )
  86:         return true;
  87:     return false;
  88: }
  89:  
  90: template<typename T>
  91: inline bool tIspunct(T c)
  92: {
  93:     if( tIsprint (c) &&
  94:         !tIslower(c) &&
  95:         !tIsupper(c) &&
  96:         !tIsspace(c) &&
  97:         !tIsdigit(c) )
  98:         return true;
  99:     return false;
 100: }
 101:  
 102: template<typename T>
 103: inline T tToAscii(T arg)
 104: {
 105:     return arg & 0x7F; // Decimal 127  ,Bin 0111 1111
 106: }
 107:  
 108: template<typename T>
 109: inline T tToLower(T arg)
 110: {
 111:     if( (unsigned int)(arg - _T('A')) < 26u )
 112:         arg += _T('a') - _T('A');
 113:  
 114:     return arg;
 115: }
 116:  
 117: template<typename T>
 118: inline T tToUpper(T arg)
 119: {
 120:     if ( (unsigned int)(arg - _T('a')) < 26u )
 121:         arg += _T('A') - _T('a');
 122:  
 123:     return arg;
 124: }

这些都很简单,也不需要做任何解释了,如果有疑惑,可以从这里了解更具体的细节。在下一节中,我们将重点介绍String Manipulation的各种函数。好了,Data Conversion部分结束了!