ctype.h
中的各种 is...
函数(例如 isalpha
、isdigit
)不是完全可以预测。它们采用 int
参数,但期望字符值在 unsigned char
范围内,因此在 char
已签名的平台上,传递一个 char
值直接可能会导致不需要的符号扩展。我相信处理此问题的典型方法是首先显式转换为 unsigned char
。
好的,但是处理 wctype.h
中的各种 isw...
函数的正确、可移植的方法是什么? wchar_t
和 char
一样,也可以是有符号或无符号的,但是因为 wchar_t
本身就是一个 typedef
,一个类型名unsigned wchar_t
是非法的。
最佳答案
这不就是吗wint_t
是为了? iswXxxxx()
函数采用 wint_t
输入:
ISO 9899:1999 的各个部分都涵盖了这一点,逆向计算:
§7.25 Wide character classification and mapping utilities
<wctype.h>
§7.25.2.1.1 The iswalnum function
Synopsis
#include <wctype.h> int iswalnum(wint_t wc);
Description
The iswalnum function tests for any wide character for which iswalpha or iswdigit is true.
§7.24 Extended multibyte and wide character utilities
<wchar.h>
§7.24.1 Introduction:
wint_t
which is an integer type unchanged by default argument promotions that can hold any value corresponding to members of the extended character set, as well as at least one value that does not correspond to any member of the extended character set (see WEOF below);269)
269)
wchar_t
andwint_t
can be the same integer type.
“默认参数提升不变”应该意味着它必须与 int
一样大, 虽然它可能是 short
或 unsigned short
如果sizeof(short) == sizeof(int)
(现在这种情况很少见,尽管对于某些 16 位系统来说确实如此)。
§7.17 Common definitions
<stddef.h>
wchar_t
which is an integer type whose range of values can represent distinct codes for all members of the largest extended character set specified among the supported locales; the null character shall have the code value zero and each member of the basic character set shall have a code value equal to its value when used as the lone character in an integer character constant.
只要把值传给iswalnum()
或其亲属是有效的 wchar_t
或 WEOF,该函数将正常工作。如果您凭空制造了值并设法弄错了值,则会出现未定义的行为。
关于c - 应该如何正确使用 wctype.h 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468306/