uClibc_ctype.h 6.5 KB

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