最近在使用 _toupper 这个函数时,发现了一个微软的一个非常低级的bug,带入如下:
char toUpper = _toupper('t');
char upper = _toupper('T');
结果确是: toUpper is T, upper is 4.
看来, _toupper这个函数的实现可能比较笨,只要是传进来,就直接进行减0x20操作,不管你传的是否是小写。
MSDN的帮助如下:
int toupper( int c ); int _toupper( int c ); int towupper( wint_t c ); int _toupper_l( int c , _locale_t locale ); int _towupper_l( wint_t c , _locale_t locale );
Each of these routines converts a copy of c, if possible, and returns the result.
If c is a wide character for which iswlower is nonzero and there is a corresponding wide character for which iswupper is nonzero, towupper returns the corresponding wide character; otherwise, towupper returns c unchanged.
There is no return value reserved to indicate an error.
In order for toupper to give the expected results, __isascii and islower must both return nonzero.
也没有说使用的注意事项。 可以看出,这应该是一个bug! 希望看到此文章的人,能够注意这个函数,以防用错!
Linux 系统的说明很清楚,APP应该保证传入的是大写字母。 NAME _toupper - transliterate lowercase characters to uppercase SYNOPSIS #include int _toupper(int c); DESCRIPTION The _toupper() macro shall be equivalent to toupper() except that the application shall ensure that the argument c is a lowercase letter.