uClibc_ctype.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. extern int isalnum(int c) __THROW;
  88. extern int isalpha(int c) __THROW;
  89. #ifdef __USE_ISOC99
  90. extern int isblank(int c) __THROW;
  91. #endif
  92. extern int iscntrl(int c) __THROW;
  93. extern int isdigit(int c) __THROW;
  94. extern int isgraph(int c) __THROW;
  95. extern int islower(int c) __THROW;
  96. extern int isprint(int c) __THROW;
  97. extern int ispunct(int c) __THROW;
  98. extern int isspace(int c) __THROW;
  99. extern int isupper(int c) __THROW;
  100. extern int isxdigit(int c) __THROW;
  101. extern int tolower(int c) __THROW;
  102. extern int toupper(int c) __THROW;
  103. #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  104. extern int isascii(int c) __THROW;
  105. extern int toascii(int c) __THROW;
  106. #endif
  107. #if defined _LIBC && (defined NOT_IN_libc || defined IS_IN_libc)
  108. /* These are uClibc-specific. */
  109. # define __isdigit_char(c) ((unsigned char)((c) - '0') <= 9)
  110. # define __isdigit_int(c) ((unsigned int)((c) - '0') <= 9)
  111. #endif
  112. /* Now some non-ansi/iso c99 macros. */
  113. #define __isascii(c) (((c) & ~0x7f) == 0)
  114. #define __toascii(c) ((c) & 0x7f)
  115. /* Works correctly *only* on lowercase letters! */
  116. #define _toupper(c) ((c) ^ 0x20)
  117. /* Works correctly *only* on letters (of any case) and numbers */
  118. #define _tolower(c) ((c) | 0x20)
  119. __END_DECLS
  120. /**********************************************************************/
  121. #ifdef __GNUC__
  122. # define __body_C_macro(f,args) __C_ ## f args
  123. # define __body(f,c) \
  124. (__extension__ ({ \
  125. int __res; \
  126. if (sizeof(c) > sizeof(char)) { \
  127. int __c = (c); \
  128. __res = __body_C_macro(f,(__c)); \
  129. } else { \
  130. unsigned char __c = (c); \
  131. __res = __body_C_macro(f,(__c)); \
  132. } \
  133. __res; \
  134. }))
  135. # define __isspace(c) __body(isspace,c)
  136. # define __isblank(c) __body(isblank,c)
  137. # define __isdigit(c) __body(isdigit,c)
  138. # define __isxdigit(c) __body(isxdigit,c)
  139. # define __iscntrl(c) __body(iscntrl,c)
  140. # define __isalpha(c) __body(isalpha,c)
  141. # define __isalnum(c) __body(isalnum,c)
  142. # define __isprint(c) __body(isprint,c)
  143. # define __islower(c) __body(islower,c)
  144. # define __isupper(c) __body(isupper,c)
  145. # define __ispunct(c) __body(ispunct,c)
  146. # define __isgraph(c) __body(isgraph,c)
  147. //locale-aware ctype.h has no __tolower, why stub locale
  148. //tries to have it? remove after 0.9.31
  149. //# define __tolower(c) __body(tolower,c)
  150. //# define __toupper(c) __body(toupper,c)
  151. # if !defined __NO_CTYPE && !defined __cplusplus
  152. # define isspace(c) __isspace(c)
  153. # define isblank(c) __isblank(c)
  154. # define isdigit(c) __isdigit(c)
  155. # define isxdigit(c) __isxdigit(c)
  156. # define iscntrl(c) __iscntrl(c)
  157. # define isalpha(c) __isalpha(c)
  158. # define isalnum(c) __isalnum(c)
  159. # define isprint(c) __isprint(c)
  160. # define islower(c) __islower(c)
  161. # define isupper(c) __isupper(c)
  162. # define ispunct(c) __ispunct(c)
  163. # define isgraph(c) __isgraph(c)
  164. # define tolower(c) __body(tolower,c)
  165. # define toupper(c) __body(toupper,c)
  166. # endif
  167. #else /* !_GNUC__ */
  168. # if !defined __NO_CTYPE && !defined __cplusplus
  169. /* These macros should be safe from side effects!
  170. * (not all __C_xxx macros are) */
  171. # define isdigit(c) __C_isdigit(c)
  172. # define isalpha(c) __C_isalpha(c)
  173. # define isprint(c) __C_isprint(c)
  174. # define islower(c) __C_islower(c)
  175. # define isupper(c) __C_isupper(c)
  176. # define isgraph(c) __C_isgraph(c)
  177. # endif
  178. #endif /* __GNUC__ */
  179. /**********************************************************************/
  180. #endif /* __UCLIBC_GEN_LOCALE */
  181. #endif /* _BITS_UCLIBC_CTYPE_H */