ctype.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef _CTYPE_H_
  2. #define _CTYPE_H_
  3. #include "_ansi.h"
  4. _BEGIN_STD_C
  5. int _EXFUN(isalnum, (int __c));
  6. int _EXFUN(isalpha, (int __c));
  7. int _EXFUN(iscntrl, (int __c));
  8. int _EXFUN(isdigit, (int __c));
  9. int _EXFUN(isgraph, (int __c));
  10. int _EXFUN(islower, (int __c));
  11. int _EXFUN(isprint, (int __c));
  12. int _EXFUN(ispunct, (int __c));
  13. int _EXFUN(isspace, (int __c));
  14. int _EXFUN(isupper, (int __c));
  15. int _EXFUN(isxdigit,(int __c));
  16. int _EXFUN(tolower, (int __c));
  17. int _EXFUN(toupper, (int __c));
  18. #if !defined(__STRICT_ANSI__) || defined(__cplusplus) || __STDC_VERSION__ >= 199901L
  19. int _EXFUN(isblank, (int __c));
  20. #endif
  21. #ifndef __STRICT_ANSI__
  22. int _EXFUN(isascii, (int __c));
  23. int _EXFUN(toascii, (int __c));
  24. #define _tolower(__c) ((unsigned char)(__c) - 'A' + 'a')
  25. #define _toupper(__c) ((unsigned char)(__c) - 'a' + 'A')
  26. #endif
  27. #define _U 01
  28. #define _L 02
  29. #define _N 04
  30. #define _S 010
  31. #define _P 020
  32. #define _C 040
  33. #define _X 0100
  34. #define _B 0200
  35. #ifndef _MB_CAPABLE
  36. _CONST
  37. #endif
  38. extern __IMPORT char * _CONST __ctype_ptr__;
  39. #ifndef __cplusplus
  40. /* These macros are intentionally written in a manner that will trigger
  41. a gcc -Wall warning if the user mistakenly passes a 'char' instead
  42. of an int containing an 'unsigned char'. Note that the sizeof will
  43. always be 1, which is what we want for mapping EOF to __ctype_ptr__[0];
  44. the use of a raw index inside the sizeof triggers the gcc warning if
  45. __c was of type char, and sizeof masks side effects of the extra __c.
  46. Meanwhile, the real index to __ctype_ptr__+1 must be cast to int,
  47. since isalpha(0x100000001LL) must equal isalpha(1), rather than being
  48. an out-of-bounds reference on a 64-bit machine. */
  49. #define __ctype_lookup(__c) ((__ctype_ptr__+sizeof(""[__c]))[(int)(__c)])
  50. #define isalpha(__c) (__ctype_lookup(__c)&(_U|_L))
  51. #define isupper(__c) ((__ctype_lookup(__c)&(_U|_L))==_U)
  52. #define islower(__c) ((__ctype_lookup(__c)&(_U|_L))==_L)
  53. #define isdigit(__c) (__ctype_lookup(__c)&_N)
  54. #define isxdigit(__c) (__ctype_lookup(__c)&(_X|_N))
  55. #define isspace(__c) (__ctype_lookup(__c)&_S)
  56. #define ispunct(__c) (__ctype_lookup(__c)&_P)
  57. #define isalnum(__c) (__ctype_lookup(__c)&(_U|_L|_N))
  58. #define isprint(__c) (__ctype_lookup(__c)&(_P|_U|_L|_N|_B))
  59. #define isgraph(__c) (__ctype_lookup(__c)&(_P|_U|_L|_N))
  60. #define iscntrl(__c) (__ctype_lookup(__c)&_C)
  61. #if defined(__GNUC__) && \
  62. (!defined(__STRICT_ANSI__) || __STDC_VERSION__ >= 199901L)
  63. #define isblank(__c) \
  64. __extension__ ({ __typeof__ (__c) __x = (__c); \
  65. (__ctype_lookup(__x)&_B) || (int) (__x) == '\t';})
  66. #endif
  67. /* Non-gcc versions will get the library versions, and will be
  68. slightly slower. These macros are not NLS-aware so they are
  69. disabled if the system supports the extended character sets. */
  70. # if defined(__GNUC__)
  71. # if !defined (_MB_EXTENDED_CHARSETS_ISO) && !defined (_MB_EXTENDED_CHARSETS_WINDOWS)
  72. # define toupper(__c) \
  73. __extension__ ({ __typeof__ (__c) __x = (__c); \
  74. islower (__x) ? (int) __x - 'a' + 'A' : (int) __x;})
  75. # define tolower(__c) \
  76. __extension__ ({ __typeof__ (__c) __x = (__c); \
  77. isupper (__x) ? (int) __x - 'A' + 'a' : (int) __x;})
  78. # else /* _MB_EXTENDED_CHARSETS* */
  79. /* Allow a gcc warning if the user passed 'char', but defer to the
  80. function. */
  81. # define toupper(__c) \
  82. __extension__ ({ __typeof__ (__c) __x = (__c); \
  83. (void) __ctype_ptr__[__x]; (toupper) (__x);})
  84. # define tolower(__c) \
  85. __extension__ ({ __typeof__ (__c) __x = (__c); \
  86. (void) __ctype_ptr__[__x]; (tolower) (__x);})
  87. # endif /* _MB_EXTENDED_CHARSETS* */
  88. # endif /* __GNUC__ */
  89. #endif /* !__cplusplus */
  90. #ifndef __STRICT_ANSI__
  91. #define isascii(__c) ((unsigned)(__c)<=0177)
  92. #define toascii(__c) ((__c)&0177)
  93. #endif
  94. /* For C++ backward-compatibility only. */
  95. extern __IMPORT _CONST char _ctype_[];
  96. _END_STD_C
  97. #endif /* _CTYPE_H_ */