宽字符串忽略大小写比较的实现(原)
孙文涛 2008-07-24
在Mac机器平台上没有wcsicmp 或 wcscasecmp之类的函数实现对宽字符忽略大小写的比较,所以实现了好几种方法。
一个自然的思路是:
(1) wcscpy 原字符串到tmp字符串;
(2) tolower tmp字符串;
(3) 然后调用仅存的wcscmp 完成比较。
我曾经实现过几次这个步骤,一个是实现起来比较繁琐,效率等方面更是不好说。
今天晚上算是花了些时间,在google code中search了好些代码,终于实现了一个较好的版本。
 #include <stdlib.h>  // for wcstombs
#include <stdlib.h>  // for wcstombs #include <wctype.h>  // for towlower
#include <wctype.h>  // for towlower #include <wchar.h>   // for wchar_t
#include <wchar.h>   // for wchar_t #include <stdio.h>
#include <stdio.h>   
 int wcscasecmp(const wchar_t* cs,const wchar_t * ct)
int wcscasecmp(const wchar_t* cs,const wchar_t * ct)

 {
{ while (towlower(*cs) == towlower(*ct))
 while (towlower(*cs) == towlower(*ct))
 
    {
{ if (*cs == 0)
      if (*cs == 0) return 0;
         return 0; cs++;
      cs++; ct++;
      ct++; }
   } return towlower(*cs) - towlower(*ct);
   return towlower(*cs) - towlower(*ct); }
}

 int main()
int main()

 {
{ wchar_t mpString[] = L"Hello World";
    wchar_t mpString[] = L"Hello World"; wchar_t strMpString[] = L"HELLO WORLD";
    wchar_t strMpString[] = L"HELLO WORLD"; 
     int ret = wcscasecmp(mpString, strMpString);
    int ret = wcscasecmp(mpString, strMpString); if(! ret)
    if(! ret)
 
     {
{ printf("Year! Equals!\n");
        printf("Year! Equals!\n"); }
    } else
    else
 
     {
{ printf("Oops! Not equals!\n");
        printf("Oops! Not equals!\n"); }
    } 
     // after conversion
    // after conversion wprintf(L"after conversion mpString = %s \n", mpString);
    wprintf(L"after conversion mpString = %s \n", mpString); wprintf(L"after conversion strMpString = %s \n", strMpString);
    wprintf(L"after conversion strMpString = %s \n", strMpString); }
}
体会:
(1) 以后如果碰到类似的问题,多用Google Code搜索;
(2) Apple Mac OS X的开发,基本的C/C++函数的查询在这个网页,我平时需要反复查询C/C++ API,所以这个网站比较实用的。
http://developer.apple.com/DOCUMENTATION/Darwin/Reference/ManPages/index.html#//apple_ref/doc/framework/manpages