uClibc_ctype.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #warning uClibc_ctype.h is deprecated
  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. /* The values for wctype_t. */
  77. enum {
  78. _CTYPE_unclassified = 0,
  79. _CTYPE_isalnum,
  80. _CTYPE_isalpha,
  81. _CTYPE_isblank,
  82. _CTYPE_iscntrl,
  83. _CTYPE_isdigit,
  84. _CTYPE_isgraph,
  85. _CTYPE_islower,
  86. _CTYPE_isprint,
  87. _CTYPE_ispunct,
  88. _CTYPE_isspace,
  89. _CTYPE_isupper,
  90. _CTYPE_isxdigit /* _MUST_ be last of the standard classes! */
  91. };
  92. /* The following is used to implement wctype(), but it is defined
  93. * here because the ordering must agree with that of the enumeration
  94. * above (ignoring unclassified). */
  95. #define __CTYPE_TYPESTRING \
  96. "\6alnum\0\6alpha\0\6blank\0\6cntrl\0\6digit\0\6graph\0\6lower\0" \
  97. "\6print\0\6punct\0\6space\0\6upper\0\7xdigit\0\0"
  98. /* Used in implementing iswctype(), but defined here as it must agree
  99. * in ordering with the string above. */
  100. #define __CTYPE_RANGES \
  101. 0, -1, /* unclassified */ \
  102. 1, __CTYPE_digit - 1, /* alnum */ \
  103. 1, __CTYPE_alpha_upper - 1, /* alpha */ \
  104. __CTYPE_print_space_blank, 5, /* blank -- also must be odd! */ \
  105. __CTYPE_cntrl_space_nonblank, 2, /* cntrl */ \
  106. __CTYPE_digit, 0, /* digit */ \
  107. 1, __CTYPE_graph - 1, /* graph */ \
  108. __CTYPE_alpha_lower, 1, /* lower */ \
  109. 1, __CTYPE_print_space_blank - 1, /* print */ \
  110. __CTYPE_punct, 0, /* punct */ \
  111. __CTYPE_print_space_nonblank, 5, /* space */ \
  112. __CTYPE_alpha_upper_lower, 1, /* upper */ \
  113. /* No entry for xdigit as it is handled specially. */
  114. #define _CTYPE_iswalnum _CTYPE_isalnum
  115. #define _CTYPE_iswalpha _CTYPE_isalpha
  116. #define _CTYPE_iswblank _CTYPE_isblank
  117. #define _CTYPE_iswcntrl _CTYPE_iscntrl
  118. #define _CTYPE_iswdigit _CTYPE_isdigit
  119. #define _CTYPE_iswgraph _CTYPE_isgraph
  120. #define _CTYPE_iswlower _CTYPE_islower
  121. #define _CTYPE_iswprint _CTYPE_isprint
  122. #define _CTYPE_iswpunct _CTYPE_ispunct
  123. #define _CTYPE_iswspace _CTYPE_isspace
  124. #define _CTYPE_iswupper _CTYPE_isupper
  125. #define _CTYPE_iswxdigit _CTYPE_isxdigit
  126. /* The following is used to implement wctrans(). */
  127. enum {
  128. _CTYPE_tolower = 1,
  129. _CTYPE_toupper,
  130. _CTYPE_totitle
  131. };
  132. #define __CTYPE_TRANSTRING "\10tolower\0\10toupper\0\10totitle\0\0"
  133. /* Now define some ctype macros valid for the C/POSIX locale. */
  134. /* ASCII ords of \t, \f, \n, \r, and \v are 9, 12, 10, 13, 11 respectively. */
  135. #define __C_isspace(c) \
  136. ((sizeof(c) == sizeof(char)) \
  137. ? ((((c) == ' ') || (((unsigned char)((c) - 9)) <= (13 - 9)))) \
  138. : ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9)))))
  139. #define __C_isblank(c) (((c) == ' ') || ((c) == '\t'))
  140. #define __C_isdigit(c) \
  141. ((sizeof(c) == sizeof(char)) \
  142. ? (((unsigned char)((c) - '0')) < 10) \
  143. : (((unsigned int)((c) - '0')) < 10))
  144. #define __C_isxdigit(c) \
  145. (__C_isdigit(c) \
  146. || ((sizeof(c) == sizeof(char)) \
  147. ? (((unsigned char)((((c)) | 0x20) - 'a')) < 6) \
  148. : (((unsigned int)((((c)) | 0x20) - 'a')) < 6)))
  149. #define __C_iscntrl(c) \
  150. ((sizeof(c) == sizeof(char)) \
  151. ? ((((unsigned char)(c)) < 0x20) || ((c) == 0x7f)) \
  152. : ((((unsigned int)(c)) < 0x20) || ((c) == 0x7f)))
  153. #define __C_isalpha(c) \
  154. ((sizeof(c) == sizeof(char)) \
  155. ? (((unsigned char)(((c) | 0x20) - 'a')) < 26) \
  156. : (((unsigned int)(((c) | 0x20) - 'a')) < 26))
  157. #define __C_isalnum(c) (__C_isalpha(c) || __C_isdigit(c))
  158. #define __C_isprint(c) \
  159. ((sizeof(c) == sizeof(char)) \
  160. ? (((unsigned char)((c) - 0x20)) <= (0x7e - 0x20)) \
  161. : (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20)))
  162. #define __C_islower(c) \
  163. ((sizeof(c) == sizeof(char)) \
  164. ? (((unsigned char)((c) - 'a')) < 26) \
  165. : (((unsigned int)((c) - 'a')) < 26))
  166. #define __C_isupper(c) \
  167. ((sizeof(c) == sizeof(char)) \
  168. ? (((unsigned char)((c) - 'A')) < 26) \
  169. : (((unsigned int)((c) - 'A')) < 26))
  170. #define __C_ispunct(c) \
  171. ((!__C_isalnum(c)) \
  172. && ((sizeof(c) == sizeof(char)) \
  173. ? (((unsigned char)((c) - 0x21)) <= (0x7e - 0x21)) \
  174. : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21))))
  175. #define __C_isgraph(c) \
  176. ((sizeof(c) == sizeof(char)) \
  177. ? (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)) \
  178. : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)))
  179. #define __C_tolower(c) (__C_isupper(c) ? ((c) | 0x20) : (c))
  180. #define __C_toupper(c) (__C_islower(c) ? ((c) ^ 0x20) : (c))
  181. #define __C_isxlower(c) \
  182. (__C_isdigit(c) \
  183. || ((sizeof(c) == sizeof(char)) \
  184. ? (((unsigned char)(((c)) - 'a')) < 6) \
  185. : (((unsigned int)(((c)) - 'a')) < 6)))
  186. #define __C_isxupper(c) \
  187. (__C_isdigit(c) \
  188. || ((sizeof(c) == sizeof(char)) \
  189. ? (((unsigned char)(((c)) - 'A')) < 6) \
  190. : (((unsigned int)(((c)) - 'A')) < 6)))
  191. /* TODO: Replace the above with expressions like the following? */
  192. /* #define __C_isdigit(c) ((sizeof(c) == sizeof(char)) \ */
  193. /* ? (((unsigned char)((c) - '0')) < 10) \ */
  194. /* : (((unsigned int)((c) - '0')) < 10)) */
  195. /* Similarly, define some wctype macros valid for the C/POSIX locale. */
  196. /* First, we need some way to make sure the arg is in range. */
  197. #define __C_classed(c) \
  198. ((sizeof(c) <= sizeof(int)) || (c == ((unsigned char)c)))
  199. #define __C_iswspace(c) (__C_classed(c) && __C_isspace(c))
  200. #define __C_iswblank(c) (__C_classed(c) && __C_isblank(c))
  201. #define __C_iswdigit(c) (__C_classed(c) && __C_isdigit(c))
  202. #define __C_iswxdigit(c) (__C_classed(c) && __C_isxdigit(c))
  203. #define __C_iswcntrl(c) (__C_classed(c) && __C_iscntrl(c))
  204. #define __C_iswalpha(c) (__C_classed(c) && __C_isalpha(c))
  205. #define __C_iswalnum(c) (__C_classed(c) && __C_isalnum(c))
  206. #define __C_iswprint(c) (__C_classed(c) && __C_isprint(c))
  207. #define __C_iswlower(c) (__C_classed(c) && __C_islower(c))
  208. #define __C_iswupper(c) (__C_classed(c) && __C_isupper(c))
  209. #define __C_iswpunct(c) (__C_classed(c) && __C_ispunct(c))
  210. #define __C_iswgraph(c) (__C_classed(c) && __C_isgraph(c))
  211. #define __C_towlower(c) \
  212. ((__C_classed(c) && __C_isupper(c)) ? ((c) | 0x20) : (c))
  213. #define __C_towupper(c) \
  214. ((__C_classed(c) && __C_islower(c)) ? ((c) ^ 0x20) : (c))
  215. /* Now define some macros to aviod the extra wrapper-function call. */
  216. #define __iswalnum(c) iswctype(c, _CTYPE_iswalnum)
  217. #define __iswalpha(c) iswctype(c, _CTYPE_iswalpha)
  218. #define __iswblank(c) iswctype(c, _CTYPE_iswblank)
  219. #define __iswcntrl(c) iswctype(c, _CTYPE_iswcntrl)
  220. #define __iswgraph(c) iswctype(c, _CTYPE_iswgraph)
  221. #define __iswlower(c) iswctype(c, _CTYPE_iswlower)
  222. #define __iswprint(c) iswctype(c, _CTYPE_iswprint)
  223. #define __iswpunct(c) iswctype(c, _CTYPE_iswpunct)
  224. #define __iswspace(c) iswctype(c, _CTYPE_iswspace)
  225. #define __iswupper(c) iswctype(c, _CTYPE_iswupper)
  226. #define __iswdigit(c) __C_iswdigit(c)
  227. #define __iswxdigit(c) __C_iswxdigit(c)
  228. #endif /* _BITS_CTYPE_H */