uClibc_ctype.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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_CTYPE_H
  32. #define _BITS_CTYPE_H
  33. #ifdef __UCLIBC_GEN_LOCALE
  34. /* Taking advantage of the C99 mutual-exclusion guarantees for the various
  35. * (w)ctype classes, including the descriptions of printing and control
  36. * (w)chars, we can place each in one of the following mutually-exlusive
  37. * subsets. Since there are less than 16, we can store the data for
  38. * each (w)chars in a nibble. In contrast, glibc uses an unsigned int
  39. * per (w)char, with one bit flag for each is* type. While this allows
  40. * a simple '&' operation to determine the type vs. a range test and a
  41. * little special handling for the "blank" and "xdigit" types in my
  42. * approach, it also uses 8 times the space for the tables on the typical
  43. * 32-bit archs we supported.*/
  44. enum {
  45. __CTYPE_unclassified = 0,
  46. __CTYPE_alpha_nonupper_nonlower,
  47. __CTYPE_alpha_lower,
  48. __CTYPE_alpha_upper_lower,
  49. __CTYPE_alpha_upper,
  50. __CTYPE_digit,
  51. __CTYPE_punct,
  52. __CTYPE_graph,
  53. __CTYPE_print_space_nonblank,
  54. __CTYPE_print_space_blank,
  55. __CTYPE_space_nonblank_noncntrl,
  56. __CTYPE_space_blank_noncntrl,
  57. __CTYPE_cntrl_space_nonblank,
  58. __CTYPE_cntrl_space_blank,
  59. __CTYPE_cntrl_nonspace
  60. };
  61. /* Some macros that test for various (w)ctype classes when passed one of the
  62. * designator values enumerated above. */
  63. #define __CTYPE_isalnum(D) ((unsigned int)(D-1) <= (__CTYPE_digit-1))
  64. #define __CTYPE_isalpha(D) ((unsigned int)(D-1) <= (__CTYPE_alpha_upper-1))
  65. #define __CTYPE_isblank(D) \
  66. ((((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5) && (D & 1))
  67. #define __CTYPE_iscntrl(D) (((unsigned int)(D - __CTYPE_cntrl_space_nonblank)) <= 2)
  68. #define __CTYPE_isdigit(D) (D == __CTYPE_digit)
  69. #define __CTYPE_isgraph(D) ((unsigned int)(D-1) <= (__CTYPE_graph-1))
  70. #define __CTYPE_islower(D) (((unsigned int)(D - __CTYPE_alpha_lower)) <= 1)
  71. #define __CTYPE_isprint(D) ((unsigned int)(D-1) <= (__CTYPE_print_space_blank-1))
  72. #define __CTYPE_ispunct(D) (D == __CTYPE_punct)
  73. #define __CTYPE_isspace(D) (((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5)
  74. #define __CTYPE_isupper(D) (((unsigned int)(D - __CTYPE_alpha_upper_lower)) <= 1)
  75. /* #define __CTYPE_isxdigit(D) -- isxdigit is untestable this way.
  76. * But that's ok as isxdigit() (and isdigit() too) are locale-invariant. */
  77. #else /* __UCLIBC_GEN_LOCALE *****************************************/
  78. /* Define some ctype macros valid for the C/POSIX locale. */
  79. /* ASCII ords of \t, \f, \n, \r, and \v are 9, 12, 10, 13, 11 respectively. */
  80. #define __C_isspace(c) \
  81. ((sizeof(c) == sizeof(char)) \
  82. ? ((((c) == ' ') || (((unsigned char)((c) - 9)) <= (13 - 9)))) \
  83. : ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9)))))
  84. #define __C_isblank(c) (((c) == ' ') || ((c) == '\t'))
  85. #define __C_isdigit(c) \
  86. ((sizeof(c) == sizeof(char)) \
  87. ? (((unsigned char)((c) - '0')) < 10) \
  88. : (((unsigned int)((c) - '0')) < 10))
  89. #define __C_isxdigit(c) \
  90. (__C_isdigit(c) \
  91. || ((sizeof(c) == sizeof(char)) \
  92. ? (((unsigned char)((((c)) | 0x20) - 'a')) < 6) \
  93. : (((unsigned int)((((c)) | 0x20) - 'a')) < 6)))
  94. #define __C_iscntrl(c) \
  95. ((sizeof(c) == sizeof(char)) \
  96. ? ((((unsigned char)(c)) < 0x20) || ((c) == 0x7f)) \
  97. : ((((unsigned int)(c)) < 0x20) || ((c) == 0x7f)))
  98. #define __C_isalpha(c) \
  99. ((sizeof(c) == sizeof(char)) \
  100. ? (((unsigned char)(((c) | 0x20) - 'a')) < 26) \
  101. : (((unsigned int)(((c) | 0x20) - 'a')) < 26))
  102. #define __C_isalnum(c) (__C_isalpha(c) || __C_isdigit(c))
  103. #define __C_isprint(c) \
  104. ((sizeof(c) == sizeof(char)) \
  105. ? (((unsigned char)((c) - 0x20)) <= (0x7e - 0x20)) \
  106. : (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20)))
  107. #define __C_islower(c) \
  108. ((sizeof(c) == sizeof(char)) \
  109. ? (((unsigned char)((c) - 'a')) < 26) \
  110. : (((unsigned int)((c) - 'a')) < 26))
  111. #define __C_isupper(c) \
  112. ((sizeof(c) == sizeof(char)) \
  113. ? (((unsigned char)((c) - 'A')) < 26) \
  114. : (((unsigned int)((c) - 'A')) < 26))
  115. #define __C_ispunct(c) \
  116. ((!__C_isalnum(c)) \
  117. && ((sizeof(c) == sizeof(char)) \
  118. ? (((unsigned char)((c) - 0x21)) <= (0x7e - 0x21)) \
  119. : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21))))
  120. #define __C_isgraph(c) \
  121. ((sizeof(c) == sizeof(char)) \
  122. ? (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)) \
  123. : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)))
  124. #define __C_tolower(c) (__C_isupper(c) ? ((c) | 0x20) : (c))
  125. #define __C_toupper(c) (__C_islower(c) ? ((c) ^ 0x20) : (c))
  126. /**********************************************************************/
  127. __BEGIN_DECLS
  128. extern int isalnum(int c) __THROW;
  129. extern int isalpha(int c) __THROW;
  130. #ifdef __USE_ISOC99
  131. extern int isblank(int c) __THROW;
  132. #endif
  133. extern int iscntrl(int c) __THROW;
  134. extern int isdigit(int c) __THROW;
  135. extern int isgraph(int c) __THROW;
  136. extern int islower(int c) __THROW;
  137. extern int isprint(int c) __THROW;
  138. extern int ispunct(int c) __THROW;
  139. extern int isspace(int c) __THROW;
  140. extern int isupper(int c) __THROW;
  141. extern int isxdigit(int c) __THROW;
  142. extern int tolower(int c) __THROW;
  143. extern int toupper(int c) __THROW;
  144. #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  145. extern int isascii(int c) __THROW;
  146. extern int toascii(int c) __THROW;
  147. #endif
  148. #if defined _LIBC && (defined NOT_IN_libc || defined IS_IN_libc)
  149. /* isdigit() is really locale-invariant, so provide some small fast macros.
  150. * These are uClibc-specific. */
  151. #define __isdigit_char(C) (((unsigned char)((C) - '0')) <= 9)
  152. #define __isdigit_int(C) (((unsigned int)((C) - '0')) <= 9)
  153. #endif
  154. /* Next, some ctype macros which are valid for all supported locales. */
  155. /* WARNING: isspace and isblank need to be reverified if more 8-bit codesets
  156. * are added!!! But isdigit and isxdigit are always valid. */
  157. /* #define __isspace(c) __C_isspace(c) */
  158. /* #define __isblank(c) __C_isblank(c) */
  159. /* #define __isdigit(c) __C_isdigit(c) */
  160. /* #define __isxdigit(c) __C_isxdigit(c) */
  161. /* Now some non-ansi/iso c99 macros. */
  162. #define __isascii(c) (((c) & ~0x7f) == 0)
  163. #define __toascii(c) ((c) & 0x7f)
  164. #define _toupper(c) ((c) ^ 0x20)
  165. #define _tolower(c) ((c) | 0x20)
  166. __END_DECLS
  167. /**********************************************************************/
  168. #ifdef __GNUC__
  169. #define __body_C_macro(f,args) __C_ ## f args
  170. #define __body(f,c) \
  171. (__extension__ ({ \
  172. int __res; \
  173. if (sizeof(c) > sizeof(char)) { \
  174. int __c = (c); \
  175. __res = __body_C_macro(f,(__c)); \
  176. } else { \
  177. unsigned char __c = (c); \
  178. __res = __body_C_macro(f,(__c)); \
  179. } \
  180. __res; \
  181. }))
  182. #define __isspace(c) __body(isspace,c)
  183. #define __isblank(c) __body(isblank,c)
  184. #define __isdigit(c) __body(isdigit,c)
  185. #define __isxdigit(c) __body(isxdigit,c)
  186. #define __iscntrl(c) __body(iscntrl,c)
  187. #define __isalpha(c) __body(isalpha,c)
  188. #define __isalnum(c) __body(isalnum,c)
  189. #define __isprint(c) __body(isprint,c)
  190. #define __islower(c) __body(islower,c)
  191. #define __isupper(c) __body(isupper,c)
  192. #define __ispunct(c) __body(ispunct,c)
  193. #define __isgraph(c) __body(isgraph,c)
  194. #define __tolower(c) __body(tolower,c)
  195. #define __toupper(c) __body(toupper,c)
  196. #if !defined __NO_CTYPE && !defined __cplusplus
  197. #define isspace(c) __isspace(c)
  198. #define isblank(c) __isblank(c)
  199. #define isdigit(c) __isdigit(c)
  200. #define isxdigit(c) __isxdigit(c)
  201. #define iscntrl(c) __iscntrl(c)
  202. #define isalpha(c) __isalpha(c)
  203. #define isalnum(c) __isalnum(c)
  204. #define isprint(c) __isprint(c)
  205. #define islower(c) __islower(c)
  206. #define isupper(c) __isupper(c)
  207. #define ispunct(c) __ispunct(c)
  208. #define isgraph(c) __isgraph(c)
  209. #define tolower(c) __tolower(c)
  210. #define toupper(c) __toupper(c)
  211. #endif
  212. #else /* _GNUC__ ***************************************************/
  213. #if !defined __NO_CTYPE && !defined __cplusplus
  214. /* These macros should be safe from side effects. */
  215. #define isdigit(c) __C_isdigit(c)
  216. #define isalpha(c) __C_isalpha(c)
  217. #define isprint(c) __C_isprint(c)
  218. #define islower(c) __C_islower(c)
  219. #define isupper(c) __C_isupper(c)
  220. #define isgraph(c) __C_isgraph(c)
  221. #endif
  222. #endif /* __GNUC__ */
  223. /**********************************************************************/
  224. #endif /* __UCLIBC_GEN_LOCALE */
  225. #endif /* _BITS_CTYPE_H */