uClibc_ctype.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This 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. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  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_CTYPE_H
  31. #define _BITS_CTYPE_H
  32. #ifdef __UCLIBC_GEN_LOCALE
  33. /* Taking advantage of the C99 mutual-exclusion guarantees for the various
  34. * (w)ctype classes, including the descriptions of printing and control
  35. * (w)chars, we can place each in one of the following mutually-exlusive
  36. * subsets. Since there are less than 16, we can store the data for
  37. * each (w)chars in a nibble. In contrast, glibc uses an unsigned int
  38. * per (w)char, with one bit flag for each is* type. While this allows
  39. * a simple '&' operation to determine the type vs. a range test and a
  40. * little special handling for the "blank" and "xdigit" types in my
  41. * approach, it also uses 8 times the space for the tables on the typical
  42. * 32-bit archs we supported.*/
  43. enum {
  44. __CTYPE_unclassified = 0,
  45. __CTYPE_alpha_nonupper_nonlower,
  46. __CTYPE_alpha_lower,
  47. __CTYPE_alpha_upper_lower,
  48. __CTYPE_alpha_upper,
  49. __CTYPE_digit,
  50. __CTYPE_punct,
  51. __CTYPE_graph,
  52. __CTYPE_print_space_nonblank,
  53. __CTYPE_print_space_blank,
  54. __CTYPE_space_nonblank_noncntrl,
  55. __CTYPE_space_blank_noncntrl,
  56. __CTYPE_cntrl_space_nonblank,
  57. __CTYPE_cntrl_space_blank,
  58. __CTYPE_cntrl_nonspace
  59. };
  60. /* Some macros that test for various (w)ctype classes when passed one of the
  61. * designator values enumerated above. */
  62. #define __CTYPE_isalnum(D) ((unsigned int)(D-1) <= (__CTYPE_digit-1))
  63. #define __CTYPE_isalpha(D) ((unsigned int)(D-1) <= (__CTYPE_alpha_upper-1))
  64. #define __CTYPE_isblank(D) \
  65. ((((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5) && (D & 1))
  66. #define __CTYPE_iscntrl(D) (((unsigned int)(D - __CTYPE_cntrl_space_nonblank)) <= 2)
  67. #define __CTYPE_isdigit(D) (D == __CTYPE_digit)
  68. #define __CTYPE_isgraph(D) ((unsigned int)(D-1) <= (__CTYPE_graph-1))
  69. #define __CTYPE_islower(D) (((unsigned int)(D - __CTYPE_alpha_lower)) <= 1)
  70. #define __CTYPE_isprint(D) ((unsigned int)(D-1) <= (__CTYPE_print_space_blank-1))
  71. #define __CTYPE_ispunct(D) (D == __CTYPE_punct)
  72. #define __CTYPE_isspace(D) (((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5)
  73. #define __CTYPE_isupper(D) (((unsigned int)(D - __CTYPE_alpha_upper_lower)) <= 1)
  74. /* #define __CTYPE_isxdigit(D) -- isxdigit is untestable this way.
  75. * But that's ok as isxdigit() (and isdigit() too) are locale-invariant. */
  76. #else /* __UCLIBC_GEN_LOCALE *****************************************/
  77. /* Define some ctype macros valid for the C/POSIX locale. */
  78. /* ASCII ords of \t, \f, \n, \r, and \v are 9, 12, 10, 13, 11 respectively. */
  79. #define __C_isspace(c) \
  80. ((sizeof(c) == sizeof(char)) \
  81. ? ((((c) == ' ') || (((unsigned char)((c) - 9)) <= (13 - 9)))) \
  82. : ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9)))))
  83. #define __C_isblank(c) (((c) == ' ') || ((c) == '\t'))
  84. #define __C_isdigit(c) \
  85. ((sizeof(c) == sizeof(char)) \
  86. ? (((unsigned char)((c) - '0')) < 10) \
  87. : (((unsigned int)((c) - '0')) < 10))
  88. #define __C_isxdigit(c) \
  89. (__C_isdigit(c) \
  90. || ((sizeof(c) == sizeof(char)) \
  91. ? (((unsigned char)((((c)) | 0x20) - 'a')) < 6) \
  92. : (((unsigned int)((((c)) | 0x20) - 'a')) < 6)))
  93. #define __C_iscntrl(c) \
  94. ((sizeof(c) == sizeof(char)) \
  95. ? ((((unsigned char)(c)) < 0x20) || ((c) == 0x7f)) \
  96. : ((((unsigned int)(c)) < 0x20) || ((c) == 0x7f)))
  97. #define __C_isalpha(c) \
  98. ((sizeof(c) == sizeof(char)) \
  99. ? (((unsigned char)(((c) | 0x20) - 'a')) < 26) \
  100. : (((unsigned int)(((c) | 0x20) - 'a')) < 26))
  101. #define __C_isalnum(c) (__C_isalpha(c) || __C_isdigit(c))
  102. #define __C_isprint(c) \
  103. ((sizeof(c) == sizeof(char)) \
  104. ? (((unsigned char)((c) - 0x20)) <= (0x7e - 0x20)) \
  105. : (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20)))
  106. #define __C_islower(c) \
  107. ((sizeof(c) == sizeof(char)) \
  108. ? (((unsigned char)((c) - 'a')) < 26) \
  109. : (((unsigned int)((c) - 'a')) < 26))
  110. #define __C_isupper(c) \
  111. ((sizeof(c) == sizeof(char)) \
  112. ? (((unsigned char)((c) - 'A')) < 26) \
  113. : (((unsigned int)((c) - 'A')) < 26))
  114. #define __C_ispunct(c) \
  115. ((!__C_isalnum(c)) \
  116. && ((sizeof(c) == sizeof(char)) \
  117. ? (((unsigned char)((c) - 0x21)) <= (0x7e - 0x21)) \
  118. : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21))))
  119. #define __C_isgraph(c) \
  120. ((sizeof(c) == sizeof(char)) \
  121. ? (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)) \
  122. : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)))
  123. #define __C_tolower(c) (__C_isupper(c) ? ((c) | 0x20) : (c))
  124. #define __C_toupper(c) (__C_islower(c) ? ((c) ^ 0x20) : (c))
  125. #define __C_isxlower(c) \
  126. (__C_isdigit(c) \
  127. || ((sizeof(c) == sizeof(char)) \
  128. ? (((unsigned char)(((c)) - 'a')) < 6) \
  129. : (((unsigned int)(((c)) - 'a')) < 6)))
  130. #define __C_isxupper(c) \
  131. (__C_isdigit(c) \
  132. || ((sizeof(c) == sizeof(char)) \
  133. ? (((unsigned char)(((c)) - 'A')) < 6) \
  134. : (((unsigned int)(((c)) - 'A')) < 6)))
  135. /**********************************************************************/
  136. __BEGIN_DECLS
  137. extern int isalnum(int c) __THROW;
  138. extern int isalpha(int c) __THROW;
  139. #ifdef __USE_ISOC99
  140. extern int isblank(int c) __THROW;
  141. #endif
  142. extern int iscntrl(int c) __THROW;
  143. extern int isdigit(int c) __THROW;
  144. extern int isgraph(int c) __THROW;
  145. extern int islower(int c) __THROW;
  146. extern int isprint(int c) __THROW;
  147. extern int ispunct(int c) __THROW;
  148. extern int isspace(int c) __THROW;
  149. extern int isupper(int c) __THROW;
  150. extern int isxdigit(int c) __THROW;
  151. extern int tolower(int c) __THROW;
  152. extern int toupper(int c) __THROW;
  153. #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  154. extern int isascii(int c) __THROW;
  155. extern int toascii(int c) __THROW;
  156. #endif
  157. /* The following are included for compatibility with older versions of
  158. * uClibc; but now they're only visible if MISC funcctionality is requested.
  159. * However, as they are locale-independent, the hidden macro versions are
  160. * always present. */
  161. #ifdef __USE_MISC
  162. extern int isxlower(int c) __THROW; /* uClibc-specific. */
  163. extern int isxupper(int c) __THROW; /* uClibc-specific. */
  164. /* isdigit() is really locale-invariant, so provide some small fast macros.
  165. * These are uClibc-specific. */
  166. #define __isdigit_char(C) (((unsigned char)((C) - '0')) <= 9)
  167. #define __isdigit_int(C) (((unsigned int)((C) - '0')) <= 9)
  168. #endif
  169. /* Next, some ctype macros which are valid for all supported locales. */
  170. /* WARNING: isspace and isblank need to be reverified if more 8-bit codesets
  171. * are added!!! But isdigit and isxdigit are always valid. */
  172. /* #define __isspace(c) __C_isspace(c) */
  173. /* #define __isblank(c) __C_isblank(c) */
  174. /* #define __isdigit(c) __C_isdigit(c) */
  175. /* #define __isxdigit(c) __C_isxdigit(c) */
  176. /* Now some non-ansi/iso c99 macros. */
  177. #define __isascii(c) (((c) & ~0x7f) == 0)
  178. #define __toascii(c) ((c) & 0x7f)
  179. #define _toupper(c) ((c) ^ 0x20)
  180. #define _tolower(c) ((c) | 0x20)
  181. /* For compatibility with older versions of uClibc. Are these ever used? */
  182. #if 0
  183. #define __isxlower(c) __C_isxlower(c) /* uClibc-specific. */
  184. #define __isxupper(c) __C_isxupper(c) /* uClibc-specific. */
  185. #endif
  186. /* Apparently, glibc implements things as macros if __NO_CTYPE isn't defined.
  187. * If we don't have locale support, we'll do the same. Otherwise, we'll
  188. * only use macros for the supported-locale-invariant cases. */
  189. #ifndef __UCLIBC_HAS_LOCALE__
  190. #endif /* __UCLIBC_HAS_LOCALE__ */
  191. __END_DECLS
  192. /**********************************************************************/
  193. #ifdef __GNUC__
  194. #define __isbody_C_macro(f,args) __C_ ## f args
  195. #define __isbody(f,c) \
  196. (__extension__ ({ \
  197. int __res; \
  198. if (sizeof(c) > sizeof(char)) { \
  199. int __c = (c); \
  200. __res = __isbody_C_macro(f,(__c)); \
  201. } else { \
  202. unsigned char __c = (c); \
  203. __res = __isbody_C_macro(f,(__c)); \
  204. } \
  205. __res; \
  206. }))
  207. #define __body_C_macro(f,args) __C_ ## f args
  208. #define __body(f,c) \
  209. (__extension__ ({ \
  210. int __res; \
  211. if (sizeof(c) > sizeof(char)) { \
  212. int __c = (c); \
  213. __res = __body_C_macro(f,(__c)); \
  214. } else { \
  215. unsigned char __c = (c); \
  216. __res = __body_C_macro(f,(__c)); \
  217. } \
  218. __res; \
  219. }))
  220. #define __isspace(c) __body(isspace,c)
  221. #define __isblank(c) __body(isblank,c)
  222. #define __isdigit(c) __body(isdigit,c)
  223. #define __isxdigit(c) __body(isxdigit,c)
  224. #define __iscntrl(c) __body(iscntrl,c)
  225. #define __isalpha(c) __body(isalpha,c)
  226. #define __isalnum(c) __body(isalnum,c)
  227. #define __isprint(c) __body(isprint,c)
  228. #define __islower(c) __body(islower,c)
  229. #define __isupper(c) __body(isupper,c)
  230. #define __ispunct(c) __body(ispunct,c)
  231. #define __isgraph(c) __body(isgraph,c)
  232. #define __isxlower(c) __body(isxlower,c)
  233. #define __isxupper(c) __body(isxupper,c)
  234. #define __tolower(c) __body(tolower,c)
  235. #define __toupper(c) __body(toupper,c)
  236. #if !defined __NO_CTYPE && !defined __cplusplus
  237. #define isspace(c) __isspace(c)
  238. #define isblank(c) __isblank(c)
  239. #define isdigit(c) __isdigit(c)
  240. #define isxdigit(c) __isxdigit(c)
  241. #define iscntrl(c) __iscntrl(c)
  242. #define isalpha(c) __isalpha(c)
  243. #define isalnum(c) __isalnum(c)
  244. #define isprint(c) __isprint(c)
  245. #define islower(c) __islower(c)
  246. #define isupper(c) __isupper(c)
  247. #define ispunct(c) __ispunct(c)
  248. #define isgraph(c) __isgraph(c)
  249. #define isxlower(c) __isxlower(c)
  250. #define isxupper(c) __isxupper(c)
  251. #define tolower(c) __tolower(c)
  252. #define toupper(c) __toupper(c)
  253. #endif
  254. #else /* _GNUC__ ***************************************************/
  255. #if !defined __NO_CTYPE && !defined __cplusplus
  256. /* These macros should be safe from side effects. */
  257. #define isdigit(c) __C_isdigit(c)
  258. #define isalpha(c) __C_isalpha(c)
  259. #define isprint(c) __C_isprint(c)
  260. #define islower(c) __C_islower(c)
  261. #define isupper(c) __C_isupper(c)
  262. #define isgraph(c) __C_isgraph(c)
  263. #endif
  264. #endif /* __GNUC__ */
  265. /**********************************************************************/
  266. #endif /* __UCLIBC_GEN_LOCALE */
  267. #endif /* _BITS_CTYPE_H */