相关的仅 ASCII 技巧: 您可以检查字母 ASCII 字符通过强制使用小写字母 c |= 0x20,然后检查是否(无符号) c - 'a' <= ('z'-'a')。所以只有3个操作: OR + SUB + CMP 对常数25。当然,编译器知道如何将 (c>='a' && c<='z')优化到像这样的高度,所以最多你应该做的 c|=0x20部分自己。自己完成所有必要的强制转换是相当不方便的,特别是在默认整数提升到有符号 int的情况下。
unsigned char lcase = y|0x20;
if (lcase - 'a' <= (unsigned)('z'-'a')) { // lcase-'a' will wrap for characters below 'a'
// c is alphabetic ASCII
}
// else it's not
或者换句话说:
unsigned char lcase = y|0x20;
unsigned char alphabet_idx = lcase - 'a'; // 0-index position in the alphabet
bool alpha = alphabet_idx <= (unsigned)('z'-'a');