uClibc_ctype.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * The GNU C Library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with the GNU C Library; see the file COPYING.LIB. If
  15. * not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  18. *
  19. * Besides uClibc, I'm using this code in my libc for elks, which is
  20. * a 16-bit environment with a fairly limited compiler. It would make
  21. * things much easier for me if this file isn't modified unnecessarily.
  22. * In particular, please put any new or replacement functions somewhere
  23. * else, and modify the makefile to use your version instead.
  24. * Thanks. Manuel
  25. *
  26. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  27. #if !defined(_CTYPE_H) && !defined(_WCTYPE_H)
  28. #error Always include <{w}ctype.h> rather than <bits/uClibc_ctype.h>
  29. #endif
  30. #ifndef _BITS_UCLIBC_CTYPE_H
  31. #define _BITS_UCLIBC_CTYPE_H
  32. #ifdef __UCLIBC_GEN_LOCALE
  33. /* We are in extra/locale/gen_XXX tools build */
  34. #include "uClibc_charclass.h"
  35. #else
  36. /* Define some ctype macros valid for the C/POSIX locale. */
  37. /* ASCII ords of \t, \f, \n, \r, and \v are 9, 12, 10, 13, 11 respectively. */
  38. #define __C_isspace(c) \
  39. ((sizeof(c) == sizeof(char)) \
  40. ? ((((c) == ' ') || (((unsigned char)((c) - 9)) <= (13 - 9)))) \
  41. : ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9)))))
  42. #define __C_isblank(c) (((c) == ' ') || ((c) == '\t'))
  43. #define __C_isdigit(c) \
  44. ((sizeof(c) == sizeof(char)) \
  45. ? (((unsigned char)((c) - '0')) < 10) \
  46. : (((unsigned int)((c) - '0')) < 10))
  47. #define __C_isxdigit(c) \
  48. (__C_isdigit(c) \
  49. || ((sizeof(c) == sizeof(char)) \
  50. ? (((unsigned char)((((c)) | 0x20) - 'a')) < 6) \
  51. : (((unsigned int)((((c)) | 0x20) - 'a')) < 6)))
  52. #define __C_iscntrl(c) \
  53. ((sizeof(c) == sizeof(char)) \
  54. ? ((((unsigned char)(c)) < 0x20) || ((c) == 0x7f)) \
  55. : ((((unsigned int)(c)) < 0x20) || ((c) == 0x7f)))
  56. #define __C_isalpha(c) \
  57. ((sizeof(c) == sizeof(char)) \
  58. ? (((unsigned char)(((c) | 0x20) - 'a')) < 26) \
  59. : (((unsigned int)(((c) | 0x20) - 'a')) < 26))
  60. #define __C_isalnum(c) (__C_isalpha(c) || __C_isdigit(c))
  61. #define __C_isprint(c) \
  62. ((sizeof(c) == sizeof(char)) \
  63. ? (((unsigned char)((c) - 0x20)) <= (0x7e - 0x20)) \
  64. : (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20)))
  65. #define __C_islower(c) \
  66. ((sizeof(c) == sizeof(char)) \
  67. ? (((unsigned char)((c) - 'a')) < 26) \
  68. : (((unsigned int)((c) - 'a')) < 26))
  69. #define __C_isupper(c) \
  70. ((sizeof(c) == sizeof(char)) \
  71. ? (((unsigned char)((c) - 'A')) < 26) \
  72. : (((unsigned int)((c) - 'A')) < 26))
  73. #define __C_ispunct(c) \
  74. ((!__C_isalnum(c)) \
  75. && ((sizeof(c) == sizeof(char)) \
  76. ? (((unsigned char)((c) - 0x21)) <= (0x7e - 0x21)) \
  77. : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21))))
  78. #define __C_isgraph(c) \
  79. ((sizeof(c) == sizeof(char)) \
  80. ? (((unsigned char)((c) - 0x21)) <= (0x7e - 0x21)) \
  81. : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)))
  82. #define __C_tolower(c) (__C_isupper(c) ? ((c) | 0x20) : (c))
  83. #define __C_toupper(c) (__C_islower(c) ? ((c) ^ 0x20) : (c))
  84. /**********************************************************************/
  85. __BEGIN_DECLS
  86. #ifdef _LIBC
  87. /* These are uClibc-specific. */
  88. # define __isdigit_char(c) ((unsigned char)((c) - '0') <= 9)
  89. # define __isdigit_int(c) ((unsigned int)((c) - '0') <= 9)
  90. #endif
  91. /* Now some non-ansi/iso c99 macros. */
  92. #ifdef __UCLIBC_SUSV4_LEGACY__
  93. #define __isascii(c) (((c) & ~0x7f) == 0)
  94. #define __toascii(c) ((c) & 0x7f)
  95. /* Works correctly *only* on lowercase letters! */
  96. #define _toupper(c) ((c) ^ 0x20)
  97. /* Works correctly *only* on letters (of any case) and numbers */
  98. #define _tolower(c) ((c) | 0x20)
  99. #endif
  100. __END_DECLS
  101. /**********************************************************************/
  102. #ifdef __GNUC__
  103. # define __body_C_macro(f,args) __C_ ## f args
  104. # define __body(f,c) \
  105. (__extension__ ({ \
  106. int __res; \
  107. if (sizeof(c) > sizeof(char)) { \
  108. int __c = (c); \
  109. __res = __body_C_macro(f,(__c)); \
  110. } else { \
  111. unsigned char __c = (c); \
  112. __res = __body_C_macro(f,(__c)); \
  113. } \
  114. __res; \
  115. }))
  116. # define __isspace(c) __body(isspace,c)
  117. # define __isblank(c) __body(isblank,c)
  118. # define __isdigit(c) __body(isdigit,c)
  119. # define __isxdigit(c) __body(isxdigit,c)
  120. # define __iscntrl(c) __body(iscntrl,c)
  121. # define __isalpha(c) __body(isalpha,c)
  122. # define __isalnum(c) __body(isalnum,c)
  123. # define __isprint(c) __body(isprint,c)
  124. # define __islower(c) __body(islower,c)
  125. # define __isupper(c) __body(isupper,c)
  126. # define __ispunct(c) __body(ispunct,c)
  127. # define __isgraph(c) __body(isgraph,c)
  128. /*locale-aware ctype.h has no __tolower, why stub locale
  129. *tries to have it? remove after 0.9.31
  130. *# define __tolower(c) __body(tolower,c)
  131. *# define __toupper(c) __body(toupper,c)
  132. */
  133. /* Do not combine in one #if - unifdef tool is not that clever */
  134. # ifndef __NO_CTYPE
  135. # ifndef __cplusplus
  136. # define isspace(c) __isspace(c)
  137. # define isblank(c) __isblank(c)
  138. # define isdigit(c) __isdigit(c)
  139. # define isxdigit(c) __isxdigit(c)
  140. # define iscntrl(c) __iscntrl(c)
  141. # define isalpha(c) __isalpha(c)
  142. # define isalnum(c) __isalnum(c)
  143. # define isprint(c) __isprint(c)
  144. # define islower(c) __islower(c)
  145. # define isupper(c) __isupper(c)
  146. # define ispunct(c) __ispunct(c)
  147. # define isgraph(c) __isgraph(c)
  148. # define tolower(c) __body(tolower,c)
  149. # define toupper(c) __body(toupper,c)
  150. # endif
  151. # endif
  152. #else /* !_GNUC__ */
  153. # ifndef __NO_CTYPE
  154. # ifndef __cplusplus
  155. /* These macros should be safe from side effects!
  156. * (not all __C_xxx macros are) */
  157. # define isdigit(c) __C_isdigit(c)
  158. # define isalpha(c) __C_isalpha(c)
  159. # define isprint(c) __C_isprint(c)
  160. # define islower(c) __C_islower(c)
  161. # define isupper(c) __C_isupper(c)
  162. # define isgraph(c) __C_isgraph(c)
  163. # endif
  164. # endif
  165. #endif /* __GNUC__ */
  166. /**********************************************************************/
  167. #endif /* __UCLIBC_GEN_LOCALE */
  168. #endif /* _BITS_UCLIBC_CTYPE_H */