大小写不敏感的字符串比较函数

doom3的源码,大小写不敏感的比较两个字符串,如果相等返回0,s1>s2返回1,s1精妙吧,哈哈。。

#define INTSIGNBITNOTSET(i) ((~((const unsigned long)(i))) >> 31)
int Icmp( const char *s1, const char *s2 ) {
int c1, c2, d;

do {
c1 = *s1++;
c2 = *s2++;

d = c1 - c2;
while( d ) {
if ( c1 <= 'Z' && c1 >= 'A' ) {
d += ('a' - 'A');
if ( !d ) {
break;
}
}
if ( c2 <= 'Z' && c2 >= 'A' ) {
d -= ('a' - 'A');
if ( !d ) {
break;
}
}
return ( INTSIGNBITNOTSET( d ) << 1 ) - 1;
}
} while( c1 );

return 0; // strings are equal
}

Comments

  1. Hi! I could have sworn I've been to yoir blog before but after going through a
    few of the posts I realized it's new to me.
    Anyways, I'm certainly delighted I discovered iit and I'll be bookmarking it and checking back
    often!

    ReplyDelete

Post a Comment

Popular posts from this blog

socket close shutdown函数区别

批量在文件头插入

hash表取模技巧