uClibc_ctype.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /* Taking advantage of the C99 mutual-exclusion guarantees for the various
  33. * (w)ctype classes, including the descriptions of printing and control
  34. * (w)chars, we can place each in one of the following mutually-exlusive
  35. * subsets. Since there are less than 16, we can store the data for
  36. * each (w)chars in a nibble. In contrast, glibc uses an unsigned int
  37. * per (w)char, with one bit flag for each is* type. While this allows
  38. * a simple '&' operation to determine the type vs. a range test and a
  39. * little special handling for the "blank" and "xdigit" types in my
  40. * approach, it also uses 8 times the space for the tables on the typical
  41. * 32-bit archs we supported.*/
  42. enum {
  43. __CTYPE_unclassified = 0,
  44. __CTYPE_alpha_nonupper_nonlower,
  45. __CTYPE_alpha_lower,
  46. __CTYPE_alpha_upper_lower,
  47. __CTYPE_alpha_upper,
  48. __CTYPE_digit,
  49. __CTYPE_punct,
  50. __CTYPE_graph,
  51. __CTYPE_print_space_nonblank,
  52. __CTYPE_print_space_blank,
  53. __CTYPE_space_nonblank_noncntrl,
  54. __CTYPE_space_blank_noncntrl,
  55. __CTYPE_cntrl_space_nonblank,
  56. __CTYPE_cntrl_space_blank,
  57. __CTYPE_cntrl_nonspace,
  58. };
  59. /* Some macros that test for various (w)ctype classes when passed one of the
  60. * designator values enumerated above. */
  61. #define __CTYPE_isalnum(D) ((unsigned int)(D-1) <= (__CTYPE_digit-1))
  62. #define __CTYPE_isalpha(D) ((unsigned int)(D-1) <= (__CTYPE_alpha_upper-1))
  63. #define __CTYPE_isblank(D) \
  64. ((((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5) && (D & 1))
  65. #define __CTYPE_iscntrl(D) (((unsigned int)(D - __CTYPE_cntrl_space_nonblank)) <= 2)
  66. #define __CTYPE_isdigit(D) (D == __CTYPE_digit)
  67. #define __CTYPE_isgraph(D) ((unsigned int)(D-1) <= (__CTYPE_graph-1))
  68. #define __CTYPE_islower(D) (((unsigned int)(D - __CTYPE_alpha_lower)) <= 1)
  69. #define __CTYPE_isprint(D) ((unsigned int)(D-1) <= (__CTYPE_print_space_blank-1))
  70. #define __CTYPE_ispunct(D) (D == __CTYPE_punct)
  71. #define __CTYPE_isspace(D) (((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5)
  72. #define __CTYPE_isupper(D) (((unsigned int)(D - __CTYPE_alpha_upper_lower)) <= 1)
  73. /* #define __CTYPE_isxdigit(D) -- isxdigit is untestable this way.
  74. * But that's ok as isxdigit() (and isdigit() too) are locale-invariant. */
  75. /* The values for wctype_t. */
  76. enum {
  77. _CTYPE_unclassified = 0,
  78. _CTYPE_isalnum,
  79. _CTYPE_isalpha,
  80. _CTYPE_isblank,
  81. _CTYPE_iscntrl,
  82. _CTYPE_isdigit,
  83. _CTYPE_isgraph,
  84. _CTYPE_islower,
  85. _CTYPE_isprint,
  86. _CTYPE_ispunct,
  87. _CTYPE_isspace,
  88. _CTYPE_isupper,
  89. _CTYPE_isxdigit /* _MUST_ be last of the standard classes! */
  90. };
  91. /* The following is used to implement wctype(), but it is defined
  92. * here because the ordering must agree with that of the enumeration
  93. * above (ignoring unclassified). */
  94. #define __CTYPE_TYPESTRING \
  95. "\6alnum\0\6alpha\0\6blank\0\6cntrl\0\6digit\0\6graph\0\6lower\0" \
  96. "\6print\0\6punct\0\6space\0\6upper\0\7xdigit\0\0"
  97. /* Used in implementing iswctype(), but defined here as it must agree
  98. * in ordering with the string above. */
  99. #define __CTYPE_RANGES \
  100. 0, -1, /* unclassified */ \
  101. 1, __CTYPE_digit - 1, /* alnum */ \
  102. 1, __CTYPE_alpha_upper - 1, /* alpha */ \
  103. __CTYPE_print_space_blank, 5, /* blank -- also must be odd! */ \
  104. __CTYPE_cntrl_space_nonblank, 2, /* cntrl */ \
  105. __CTYPE_digit, 0, /* digit */ \
  106. 1, __CTYPE_graph - 1, /* graph */ \
  107. __CTYPE_alpha_lower, 1, /* lower */ \
  108. 1, __CTYPE_print_space_blank - 1, /* print */ \
  109. __CTYPE_punct, 0, /* punct */ \
  110. __CTYPE_print_space_nonblank, 5, /* space */ \
  111. __CTYPE_alpha_upper_lower, 1, /* upper */ \
  112. /* No entry for xdigit as it is handled specially. */
  113. #define _CTYPE_iswalnum _CTYPE_isalnum
  114. #define _CTYPE_iswalpha _CTYPE_isalpha
  115. #define _CTYPE_iswblank _CTYPE_isblank
  116. #define _CTYPE_iswcntrl _CTYPE_iscntrl
  117. #define _CTYPE_iswdigit _CTYPE_isdigit
  118. #define _CTYPE_iswgraph _CTYPE_isgraph
  119. #define _CTYPE_iswlower _CTYPE_islower
  120. #define _CTYPE_iswprint _CTYPE_isprint
  121. #define _CTYPE_iswpunct _CTYPE_ispunct
  122. #define _CTYPE_iswspace _CTYPE_isspace
  123. #define _CTYPE_iswupper _CTYPE_isupper
  124. #define _CTYPE_iswxdigit _CTYPE_isxdigit
  125. /* The following is used to implement wctrans(). */
  126. enum {
  127. _CTYPE_tolower = 1,
  128. _CTYPE_toupper,
  129. _CTYPE_totitle
  130. };
  131. #define __CTYPE_TRANSTRING "\10tolower\0\10toupper\0\10totitle\0\0"
  132. /* Now define some ctype macros valid for the C/POSIX locale. */
  133. /* ASCII ords of \t, \f, \n, \r, and \v are 9, 12, 10, 13, 11 respectively. */
  134. #define __C_isspace(c) \
  135. ((sizeof(c) == sizeof(char)) \
  136. ? ((((c) == ' ') || (((unsigned char)((c) - 9)) <= (13 - 9)))) \
  137. : ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9)))))
  138. #define __C_isblank(c) (((c) == ' ') || ((c) == '\t'))
  139. #define __C_isdigit(c) \
  140. ((sizeof(c) == sizeof(char)) \
  141. ? (((unsigned char)((c) - '0')) < 10) \
  142. : (((unsigned int)((c) - '0')) < 10))
  143. #define __C_isxdigit(c) \
  144. (__C_isdigit(c) \
  145. || ((sizeof(c) == sizeof(char)) \
  146. ? (((unsigned char)((((c)) | 0x20) - 'a')) < 6) \
  147. : (((unsigned int)((((c)) | 0x20) - 'a')) < 6)))
  148. #define __C_iscntrl(c) \
  149. ((sizeof(c) == sizeof(char)) \
  150. ? ((((unsigned char)(c)) < 0x20) || ((c) == 0x7f)) \
  151. : ((((unsigned int)(c)) < 0x20) || ((c) == 0x7f)))
  152. #define __C_isalpha(c) \
  153. ((sizeof(c) == sizeof(char)) \
  154. ? (((unsigned char)(((c) | 0x20) - 'a')) < 26) \
  155. : (((unsigned int)(((c) | 0x20) - 'a')) < 26))
  156. #define __C_isalnum(c) (__C_isalpha(c) || __C_isdigit(c))
  157. #define __C_isprint(c) \
  158. ((sizeof(c) == sizeof(char)) \
  159. ? (((unsigned char)((c) - 0x20)) <= (0x7e - 0x20)) \
  160. : (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20)))
  161. #define __C_islower(c) \
  162. ((sizeof(c) == sizeof(char)) \
  163. ? (((unsigned char)((c) - 'a')) < 26) \
  164. : (((unsigned int)((c) - 'a')) < 26))
  165. #define __C_isupper(c) \
  166. ((sizeof(c) == sizeof(char)) \
  167. ? (((unsigned char)((c) - 'A')) < 26) \
  168. : (((unsigned int)((c) - 'A')) < 26))
  169. #define __C_ispunct(c) \
  170. ((!__C_isalnum(c)) \
  171. && ((sizeof(c) == sizeof(char)) \
  172. ? (((unsigned char)((c) - 0x21)) <= (0x7e - 0x21)) \
  173. : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21))))
  174. #define __C_isgraph(c) \
  175. ((sizeof(c) == sizeof(char)) \
  176. ? (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)) \
  177. : (((unsigned int)((c) - 0x21)) <= (0x7e - 0x21)))
  178. #define __C_tolower(c) (__C_isupper(c) ? ((c) | 0x20) : (c))
  179. #define __C_toupper(c) (__C_islower(c) ? ((c) ^ 0x20) : (c))
  180. #define __C_isxlower(c) \
  181. (__C_isdigit(c) \
  182. || ((sizeof(c) == sizeof(char)) \
  183. ? (((unsigned char)(((c)) - 'a')) < 6) \
  184. : (((unsigned int)(((c)) - 'a')) < 6)))
  185. #define __C_isxupper(c) \
  186. (__C_isdigit(c) \
  187. || ((sizeof(c) == sizeof(char)) \
  188. ? (((unsigned char)(((c)) - 'A')) < 6) \
  189. : (((unsigned int)(((c)) - 'A')) < 6)))
  190. /* TODO: Replace the above with expressions like the following? */
  191. /* #define __C_isdigit(c) ((sizeof(c) == sizeof(char)) \ */
  192. /* ? (((unsigned char)((c) - '0')) < 10) \ */
  193. /* : (((unsigned int)((c) - '0')) < 10)) */
  194. /* Similarly, define some wctype macros valid for the C/POSIX locale. */
  195. /* First, we need some way to make sure the arg is in range. */
  196. #define __C_classed(c) \
  197. ((sizeof(c) <= sizeof(int)) || (c == ((unsigned char)c)))
  198. #define __C_iswspace(c) (__C_classed(c) && __C_isspace(c))
  199. #define __C_iswblank(c) (__C_classed(c) && __C_isblank(c))
  200. #define __C_iswdigit(c) (__C_classed(c) && __C_isdigit(c))
  201. #define __C_iswxdigit(c) (__C_classed(c) && __C_isxdigit(c))
  202. #define __C_iswcntrl(c) (__C_classed(c) && __C_iscntrl(c))
  203. #define __C_iswalpha(c) (__C_classed(c) && __C_isalpha(c))
  204. #define __C_iswalnum(c) (__C_classed(c) && __C_isalnum(c))
  205. #define __C_iswprint(c) (__C_classed(c) && __C_isprint(c))
  206. #define __C_iswlower(c) (__C_classed(c) && __C_islower(c))
  207. #define __C_iswupper(c) (__C_classed(c) && __C_isupper(c))
  208. #define __C_iswpunct(c) (__C_classed(c) && __C_ispunct(c))
  209. #define __C_iswgraph(c) (__C_classed(c) && __C_isgraph(c))
  210. #define __C_towlower(c) \
  211. ((__C_classed(c) && __C_isupper(c)) ? ((c) | 0x20) : (c))
  212. #define __C_towupper(c) \
  213. ((__C_classed(c) && __C_islower(c)) ? ((c) ^ 0x20) : (c))
  214. /* Now define some macros to aviod the extra wrapper-function call. */
  215. #define __iswalnum(c) iswctype(c, _CTYPE_iswalnum)
  216. #define __iswalpha(c) iswctype(c, _CTYPE_iswalpha)
  217. #define __iswblank(c) iswctype(c, _CTYPE_iswblank)
  218. #define __iswcntrl(c) iswctype(c, _CTYPE_iswcntrl)
  219. #define __iswgraph(c) iswctype(c, _CTYPE_iswgraph)
  220. #define __iswlower(c) iswctype(c, _CTYPE_iswlower)
  221. #define __iswprint(c) iswctype(c, _CTYPE_iswprint)
  222. #define __iswpunct(c) iswctype(c, _CTYPE_iswpunct)
  223. #define __iswspace(c) iswctype(c, _CTYPE_iswspace)
  224. #define __iswupper(c) iswctype(c, _CTYPE_iswupper)
  225. #define __iswdigit(c) __C_iswdigit(c)
  226. #define __iswxdigit(c) __C_iswxdigit(c)
  227. #endif /* _BITS_CTYPE_H */