简体   繁体   English

C中isupper的宏定义是什么?

[英]What is the macro definition of isupper in C?

I want to know how the "isupper" macro is defined in C/C++. 我想知道在C / C ++中如何定义“isupper”宏。 Could you please provide me the same or point me to available resources. 能否请您提供相同的信息或指向我可用的资源。 I tried looking at ctype.h but couldnt figure it out. 我试着查看ctype.h,但无法弄明白。

It's implementation defined -- every vendor can, and usually does, do it differently. 它是实现定义的 - 每个供应商都可以,而且通常会以不同的方式执行。

The most common usually involves a "traits" table - an array with one element for each character, the value of that element being a collection of flags indicates details about the character. 最常见的通常是“traits”表 - 一个数组,每个字符有一个元素,该元素的值是一组标志,表示有关该字符的详细信息。 An example would be: 一个例子是:

 traits[(int) 'C'] = ALPHA | UPPER | PRINTABLE;

In which case,, isupper() would be something like: 在这种情况下,isupper()将是这样的:

 #define isupper(c) ((traits[(int)(c)] & UPPER) == UPPER)

It's a function, not a macro. 这是一个功能,而不是宏。 The function definition of isupper() differs depending on things like locale and the current character set - that's why there's a function specifically for this purpose. isupper()的函数定义根据locale和当前字符集等不同而不同 - 这就是为什么有一个专门用于此目的的函数。

For ASCII, because of the way the letters are assigned, it's actually quite easy to test for this. 对于ASCII,由于字母的分配方式,实际上很容易测试。 If the ASCII code of the character falls in between 0x41 and 0x5A inclusive, then it is an upper case letter. 如果字符的ASCII码介于0x410x5A之间,则它是大写字母。

It's implementation-specific. 它是特定于实现的。 One obvious way to implement it would be: 实现它的一个明显方法是:

extern char *__isupper;
#define isupper(x) ((int)__isupper[(x)])

Where __isupper points to an array of 0's and 1's determined by the locale. 其中__isupper指向由区域设置确定的0和1的数组。 However this sort of technique has gone out of favor since accessing global variables in shared libraries is rather inefficient and creates permanent ABI requirements, and since it's incompatible with POSIX thread-local locales. 然而,这种技术已经失宠,因为访问共享库中的全局变量效率很低并且会产生永久的ABI要求,并且因为它与POSIX线程本地语言环境不兼容。

Another obvious way to implement it on ASCII-only or UTF-8-only implementations is: 在仅ASCII或仅UTF-8的实现上实现它的另一种显而易见的方法是:

#define isupper(x) ((unsigned)(x)-'A'<='Z'-'A')

It's actually fairly complicated, in GCC for instance. 实际上,它在GCC中相当复杂。 But a simple implementation of isupper could be (although it has a double-evaluation bug) most simply defined as: 但isupper的一个简单实现可能是(虽然它有一个双重评估错误),最简单的定义为:

#define isupper(c) (c >= 'A') & (c <= 'Z') #define isupper(c)(c> ='A')&(c <='Z')

http://ideone.com/GlN05 http://ideone.com/GlN05

GCC specifically checks bit 0 is 1 in the character for the current locale: GCC专门检查当前语言环境的字符中的位0是1:

(*__ctype_b_loc ())[(int) (c)] & (unsigned short int) (1 << (0)) (* __ ctype_b_loc())[(int)(c)]&(unsigned short int)(1 <<(0))

Where __ctype_b_loc() is a function that returns a pointer into an array of characters in the current locale that contains characteristics for each character in the current character set. 其中__ctype_b_loc()是一个函数,它返回指向当前语言环境中包含当前字符集中每个字符的特征的字符数组的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM