ctype.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #ifndef _CTYPE_H_
  2. #define _CTYPE_H_
  3. #include "_ansi.h"
  4. #include <sys/cdefs.h>
  5. #if __POSIX_VISIBLE >= 200809 || __MISC_VISIBLE || defined (_COMPILING_NEWLIB)
  6. #include <sys/_locale.h>
  7. #endif
  8. _BEGIN_STD_C
  9. int isalnum (int __c);
  10. int isalpha (int __c);
  11. int iscntrl (int __c);
  12. int isdigit (int __c);
  13. int isgraph (int __c);
  14. int islower (int __c);
  15. int isprint (int __c);
  16. int ispunct (int __c);
  17. int isspace (int __c);
  18. int isupper (int __c);
  19. int isxdigit (int __c);
  20. int tolower (int __c);
  21. int toupper (int __c);
  22. #if __ISO_C_VISIBLE >= 1999
  23. int isblank (int __c);
  24. #endif
  25. #if __MISC_VISIBLE || __XSI_VISIBLE
  26. int isascii (int __c);
  27. int toascii (int __c);
  28. #define _tolower(__c) ((unsigned char)(__c) - 'A' + 'a')
  29. #define _toupper(__c) ((unsigned char)(__c) - 'a' + 'A')
  30. #endif
  31. #if __POSIX_VISIBLE >= 200809
  32. extern int isalnum_l (int __c, locale_t __l);
  33. extern int isalpha_l (int __c, locale_t __l);
  34. extern int isblank_l (int __c, locale_t __l);
  35. extern int iscntrl_l (int __c, locale_t __l);
  36. extern int isdigit_l (int __c, locale_t __l);
  37. extern int isgraph_l (int __c, locale_t __l);
  38. extern int islower_l (int __c, locale_t __l);
  39. extern int isprint_l (int __c, locale_t __l);
  40. extern int ispunct_l (int __c, locale_t __l);
  41. extern int isspace_l (int __c, locale_t __l);
  42. extern int isupper_l (int __c, locale_t __l);
  43. extern int isxdigit_l(int __c, locale_t __l);
  44. extern int tolower_l (int __c, locale_t __l);
  45. extern int toupper_l (int __c, locale_t __l);
  46. #endif
  47. #if __MISC_VISIBLE
  48. extern int isascii_l (int __c, locale_t __l);
  49. extern int toascii_l (int __c, locale_t __l);
  50. #endif
  51. #define _U 01
  52. #define _L 02
  53. #define _N 04
  54. #define _S 010
  55. #define _P 020
  56. #define _C 040
  57. #define _X 0100
  58. #define _B 0200
  59. /* For C++ backward-compatibility only. */
  60. extern __IMPORT const char _ctype_[];
  61. #ifdef __HAVE_LOCALE_INFO__
  62. const char *__locale_ctype_ptr (void);
  63. #else
  64. #define __locale_ctype_ptr() _ctype_
  65. #endif
  66. # define __CTYPE_PTR (__locale_ctype_ptr ())
  67. #ifndef __cplusplus
  68. /* These macros are intentionally written in a manner that will trigger
  69. a gcc -Wall warning if the user mistakenly passes a 'char' instead
  70. of an int containing an 'unsigned char'. Note that the sizeof will
  71. always be 1, which is what we want for mapping EOF to __CTYPE_PTR[0];
  72. the use of a raw index inside the sizeof triggers the gcc warning if
  73. __c was of type char, and sizeof masks side effects of the extra __c.
  74. Meanwhile, the real index to __CTYPE_PTR+1 must be cast to int,
  75. since isalpha(0x100000001LL) must equal isalpha(1), rather than being
  76. an out-of-bounds reference on a 64-bit machine. */
  77. #define __ctype_lookup(__c) ((__CTYPE_PTR+sizeof(""[__c]))[(int)(__c)])
  78. #define isalpha(__c) (__ctype_lookup(__c)&(_U|_L))
  79. #define isupper(__c) ((__ctype_lookup(__c)&(_U|_L))==_U)
  80. #define islower(__c) ((__ctype_lookup(__c)&(_U|_L))==_L)
  81. #define isdigit(__c) (__ctype_lookup(__c)&_N)
  82. #define isxdigit(__c) (__ctype_lookup(__c)&(_X|_N))
  83. #define isspace(__c) (__ctype_lookup(__c)&_S)
  84. #define ispunct(__c) (__ctype_lookup(__c)&_P)
  85. #define isalnum(__c) (__ctype_lookup(__c)&(_U|_L|_N))
  86. #define isprint(__c) (__ctype_lookup(__c)&(_P|_U|_L|_N|_B))
  87. #define isgraph(__c) (__ctype_lookup(__c)&(_P|_U|_L|_N))
  88. #define iscntrl(__c) (__ctype_lookup(__c)&_C)
  89. #if defined(__GNUC__) && __ISO_C_VISIBLE >= 1999
  90. #define isblank(__c) \
  91. __extension__ ({ __typeof__ (__c) __x = (__c); \
  92. (__ctype_lookup(__x)&_B) || (int) (__x) == '\t';})
  93. #endif
  94. #if __POSIX_VISIBLE >= 200809
  95. #ifdef __HAVE_LOCALE_INFO__
  96. const char *__locale_ctype_ptr_l (locale_t);
  97. #else
  98. static __inline const char *
  99. __locale_ctype_ptr_l(locale_t _l)
  100. {
  101. (void)_l;
  102. return __locale_ctype_ptr();
  103. }
  104. #endif
  105. #define __ctype_lookup_l(__c,__l) ((__locale_ctype_ptr_l(__l)+sizeof(""[__c]))[(int)(__c)])
  106. #define isalpha_l(__c,__l) (__ctype_lookup_l(__c,__l)&(_U|_L))
  107. #define isupper_l(__c,__l) ((__ctype_lookup_l(__c,__l)&(_U|_L))==_U)
  108. #define islower_l(__c,__l) ((__ctype_lookup_l(__c,__l)&(_U|_L))==_L)
  109. #define isdigit_l(__c,__l) (__ctype_lookup_l(__c,__l)&_N)
  110. #define isxdigit_l(__c,__l) (__ctype_lookup_l(__c,__l)&(_X|_N))
  111. #define isspace_l(__c,__l) (__ctype_lookup_l(__c,__l)&_S)
  112. #define ispunct_l(__c,__l) (__ctype_lookup_l(__c,__l)&_P)
  113. #define isalnum_l(__c,__l) (__ctype_lookup_l(__c,__l)&(_U|_L|_N))
  114. #define isprint_l(__c,__l) (__ctype_lookup_l(__c,__l)&(_P|_U|_L|_N|_B))
  115. #define isgraph_l(__c,__l) (__ctype_lookup_l(__c,__l)&(_P|_U|_L|_N))
  116. #define iscntrl_l(__c,__l) (__ctype_lookup_l(__c,__l)&_C)
  117. #if defined(__GNUC__)
  118. #define isblank_l(__c, __l) \
  119. __extension__ ({ __typeof__ (__c) __x = (__c); \
  120. (__ctype_lookup_l(__x,__l)&_B) || (int) (__x) == '\t';})
  121. #endif
  122. #endif /* __POSIX_VISIBLE >= 200809 */
  123. #if __MISC_VISIBLE || __XSI_VISIBLE
  124. #define isascii(__c) ((unsigned)(__c)<=0177)
  125. #define toascii(__c) ((__c)&0177)
  126. #endif
  127. #if __MISC_VISIBLE
  128. #define isascii_l(__c,__l) ((__l),(unsigned)(__c)<=0177)
  129. #define toascii_l(__c,__l) ((__l),(__c)&0177)
  130. #endif
  131. /* Non-gcc versions will get the library versions, and will be
  132. slightly slower. These macros are not NLS-aware so they are
  133. disabled if the system supports the extended character sets. */
  134. # if defined(__GNUC__)
  135. # if !defined (_MB_EXTENDED_CHARSETS_ISO) && !defined (_MB_EXTENDED_CHARSETS_WINDOWS)
  136. # define toupper(__c) \
  137. __extension__ ({ __typeof__ (__c) __x = (__c); \
  138. islower (__x) ? (int) __x - 'a' + 'A' : (int) __x;})
  139. # define tolower(__c) \
  140. __extension__ ({ __typeof__ (__c) __x = (__c); \
  141. isupper (__x) ? (int) __x - 'A' + 'a' : (int) __x;})
  142. # else /* _MB_EXTENDED_CHARSETS* */
  143. /* Allow a gcc warning if the user passed 'char', but defer to the
  144. function. */
  145. # define toupper(__c) \
  146. __extension__ ({ __typeof__ (__c) __x = (__c); \
  147. (void) __CTYPE_PTR[__x]; (toupper) (__x);})
  148. # define tolower(__c) \
  149. __extension__ ({ __typeof__ (__c) __x = (__c); \
  150. (void) __CTYPE_PTR[__x]; (tolower) (__x);})
  151. # endif /* _MB_EXTENDED_CHARSETS* */
  152. # endif /* __GNUC__ */
  153. #if __POSIX_VISIBLE >= 200809
  154. #endif /* __POSIX_VISIBLE >= 200809 */
  155. #endif /* !__cplusplus */
  156. _END_STD_C
  157. #endif /* _CTYPE_H_ */