ctype.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* Copyright (C) 1991,92,93,95,96,97,98,99,2001,02
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  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. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. /*
  17. * ISO C99 Standard 7.4: Character handling <ctype.h>
  18. */
  19. #ifndef _CTYPE_H
  20. #define _CTYPE_H 1
  21. #include <features.h>
  22. #include <bits/types.h>
  23. #ifdef __UCLIBC_HAS_CTYPE_TABLES__
  24. __BEGIN_DECLS
  25. #ifndef _ISbit
  26. /* These are all the characteristics of characters.
  27. If there get to be more than 16 distinct characteristics,
  28. __ctype_mask_t will need to be adjusted. */
  29. # define _ISbit(bit) (1 << (bit))
  30. enum
  31. {
  32. _ISupper = _ISbit (0), /* UPPERCASE. */
  33. _ISlower = _ISbit (1), /* lowercase. */
  34. _ISalpha = _ISbit (2), /* Alphabetic. */
  35. _ISdigit = _ISbit (3), /* Numeric. */
  36. _ISxdigit = _ISbit (4), /* Hexadecimal numeric. */
  37. _ISspace = _ISbit (5), /* Whitespace. */
  38. _ISprint = _ISbit (6), /* Printing. */
  39. _ISgraph = _ISbit (7), /* Graphical. */
  40. _ISblank = _ISbit (8), /* Blank (usually SPC and TAB). */
  41. _IScntrl = _ISbit (9), /* Control character. */
  42. _ISpunct = _ISbit (10), /* Punctuation. */
  43. _ISalnum = _ISbit (11) /* Alphanumeric. */
  44. };
  45. #else
  46. #error _ISbit already defined!
  47. #endif /* ! _ISbit */
  48. #include <bits/uClibc_touplow.h>
  49. #ifdef __UCLIBC_HAS_CTYPE_SIGNED__
  50. # define __UCLIBC_CTYPE_IN_TO_DOMAIN(c) (((unsigned int)((c) + 128)) < 384)
  51. #else /* __UCLIBC_HAS_CTYPE_SIGNED__ */
  52. # define __UCLIBC_CTYPE_IN_TO_DOMAIN(c) (((unsigned int)(c)) < 256)
  53. #endif /* __UCLIBC_HAS_CTYPE_SIGNED__ */
  54. /* In the thread-specific locale model (see `uselocale' in <locale.h>)
  55. we cannot use global variables for these as was done in the past.
  56. Instead, the following accessor functions return the address of
  57. each variable, which is local to the current thread if multithreaded.
  58. These point into arrays of 384, so they can be indexed by any `unsigned
  59. char' value [0,255]; by EOF (-1); or by any `signed char' value
  60. [-128,-1). ISO C requires that the ctype functions work for `unsigned
  61. char' values and for EOF; we also support negative `signed char' values
  62. for broken old programs. The case conversion arrays are of `int's
  63. rather than `unsigned char's because tolower (EOF) must be EOF, which
  64. doesn't fit into an `unsigned char'. But today more important is that
  65. the arrays are also used for multi-byte character sets. */
  66. /* uClibc differences:
  67. *
  68. * When __UCLIBC_HAS_CTYPE_SIGNED is defined,
  69. *
  70. * The upper and lower mapping arrays are type int16_t, so that
  71. * they may store all char values plus EOF. The glibc reasoning
  72. * given above for these being type int is questionable, as the
  73. * ctype mapping functions map from the set of (unsigned) char
  74. * and EOF back into the set. They have no awareness of multi-byte
  75. * or wide characters.
  76. *
  77. * Otherwise,
  78. *
  79. * The ctype array is defined for -1..255.
  80. * The upper and lower mapping arrays are defined for 0..255.
  81. * The upper and lower mapping arrays are type unsigned char.
  82. */
  83. /* Pointers to the default C-locale data. */
  84. extern const __ctype_mask_t *__C_ctype_b;
  85. extern const __ctype_touplow_t *__C_ctype_toupper;
  86. extern const __ctype_touplow_t *__C_ctype_tolower;
  87. #ifdef __UCLIBC_HAS_XLOCALE__
  88. extern __const __ctype_mask_t **__ctype_b_loc (void)
  89. __attribute__ ((__const));
  90. extern __const __ctype_touplow_t **__ctype_tolower_loc (void)
  91. __attribute__ ((__const));
  92. extern __const __ctype_touplow_t **__ctype_toupper_loc (void)
  93. __attribute__ ((__const));
  94. #define __UCLIBC_CTYPE_B (*__ctype_b_loc())
  95. #define __UCLIBC_CTYPE_TOLOWER (*__ctype_tolower_loc())
  96. #define __UCLIBC_CTYPE_TOUPPER (*__ctype_toupper_loc())
  97. #else /* __UCLIBC_HAS_XLOCALE__ */
  98. /* Pointers to the current global locale data in use. */
  99. extern const __ctype_mask_t *__ctype_b;
  100. extern const __ctype_touplow_t *__ctype_toupper;
  101. extern const __ctype_touplow_t *__ctype_tolower;
  102. #define __UCLIBC_CTYPE_B (__ctype_b)
  103. #define __UCLIBC_CTYPE_TOLOWER (__ctype_tolower)
  104. #define __UCLIBC_CTYPE_TOUPPER (__ctype_toupper)
  105. #endif /* __UCLIBC_HAS_XLOCALE__ */
  106. #define __isctype(c, type) \
  107. ((__UCLIBC_CTYPE_B)[(int) (c)] & (__ctype_mask_t) type)
  108. #define __isascii(c) (((c) & ~0x7f) == 0) /* If C is a 7 bit value. */
  109. #define __toascii(c) ((c) & 0x7f) /* Mask off high bits. */
  110. #if defined _LIBC && (defined IS_IN_libc || defined NOT_IN_libc)
  111. /* isdigit() is really locale-invariant, so provide some small fast macros.
  112. * These are uClibc-specific. */
  113. #define __isdigit_char(C) (((unsigned char)((C) - '0')) <= 9)
  114. #define __isdigit_int(C) (((unsigned int)((C) - '0')) <= 9)
  115. #endif
  116. #define __exctype(name) extern int name (int) __THROW
  117. __BEGIN_NAMESPACE_STD
  118. /* The following names are all functions:
  119. int isCHARACTERISTIC(int c);
  120. which return nonzero iff C has CHARACTERISTIC.
  121. For the meaning of the characteristic names, see the `enum' above. */
  122. __exctype (isalnum);
  123. __exctype (isalpha);
  124. __exctype (iscntrl);
  125. __exctype (isdigit);
  126. __exctype (islower);
  127. __exctype (isgraph);
  128. __exctype (isprint);
  129. __exctype (ispunct);
  130. __exctype (isspace);
  131. __exctype (isupper);
  132. __exctype (isxdigit);
  133. /* Return the lowercase version of C. */
  134. extern int tolower (int __c) __THROW;
  135. /* Return the uppercase version of C. */
  136. extern int toupper (int __c) __THROW;
  137. __END_NAMESPACE_STD
  138. /* ISO C99 introduced one new function. */
  139. #ifdef __USE_ISOC99
  140. __BEGIN_NAMESPACE_C99
  141. __exctype (isblank);
  142. __END_NAMESPACE_C99
  143. #endif
  144. #ifdef __USE_GNU
  145. /* Test C for a set of character classes according to MASK. */
  146. extern int isctype (int __c, int __mask) __THROW;
  147. #endif
  148. #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  149. /* Return nonzero iff C is in the ASCII set
  150. (i.e., is no more than 7 bits wide). */
  151. extern int isascii (int __c) __THROW;
  152. /* Return the part of C that is in the ASCII set
  153. (i.e., the low-order 7 bits of C). */
  154. extern int toascii (int __c) __THROW;
  155. /* These are the same as `toupper' and `tolower' except that they do not
  156. check the argument for being in the range of a `char'. */
  157. __exctype (_toupper);
  158. __exctype (_tolower);
  159. #endif /* Use SVID or use misc. */
  160. /* This code is needed for the optimized mapping functions. */
  161. #define __tobody(c, f, a, args) \
  162. (__extension__ \
  163. ({ int __res; \
  164. if (sizeof (c) > 1) \
  165. { \
  166. if (__builtin_constant_p (c)) \
  167. { \
  168. int __c = (c); \
  169. __res = __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (a)[__c] : __c; \
  170. } \
  171. else \
  172. __res = f args; \
  173. } \
  174. else \
  175. __res = (a)[(int) (c)]; \
  176. __res; }))
  177. #if !defined __NO_CTYPE && !defined __cplusplus
  178. # define isalnum(c) __isctype((c), _ISalnum)
  179. # define isalpha(c) __isctype((c), _ISalpha)
  180. # define iscntrl(c) __isctype((c), _IScntrl)
  181. # define isdigit(c) __isctype((c), _ISdigit)
  182. # define islower(c) __isctype((c), _ISlower)
  183. # define isgraph(c) __isctype((c), _ISgraph)
  184. # define isprint(c) __isctype((c), _ISprint)
  185. # define ispunct(c) __isctype((c), _ISpunct)
  186. # define isspace(c) __isctype((c), _ISspace)
  187. # define isupper(c) __isctype((c), _ISupper)
  188. # define isxdigit(c) __isctype((c), _ISxdigit)
  189. # ifdef __USE_ISOC99
  190. # define isblank(c) __isctype((c), _ISblank)
  191. # endif
  192. # ifdef __USE_EXTERN_INLINES
  193. __extern_inline int
  194. __NTH (tolower (int __c))
  195. {
  196. return __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (__UCLIBC_CTYPE_TOLOWER)[__c] : __c;
  197. }
  198. __extern_inline int
  199. __NTH (toupper (int __c))
  200. {
  201. return __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (__UCLIBC_CTYPE_TOUPPER)[__c] : __c;
  202. }
  203. # endif
  204. # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
  205. # define tolower(c) __tobody (c, tolower, __UCLIBC_CTYPE_TOLOWER, (c))
  206. # define toupper(c) __tobody (c, toupper, __UCLIBC_CTYPE_TOUPPER, (c))
  207. # endif /* Optimizing gcc */
  208. # if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  209. # define isascii(c) __isascii (c)
  210. # define toascii(c) __toascii (c)
  211. # define _tolower(c) ((int) (__UCLIBC_CTYPE_TOLOWER)[(int) (c)])
  212. # define _toupper(c) ((int) (__UCLIBC_CTYPE_TOUPPER)[(int) (c)])
  213. # endif
  214. #endif /* Not __NO_CTYPE. */
  215. #if defined(__USE_GNU) && defined(__UCLIBC_HAS_XLOCALE__)
  216. /* The concept of one static locale per category is not very well
  217. thought out. Many applications will need to process its data using
  218. information from several different locales. Another application is
  219. the implementation of the internationalization handling in the
  220. upcoming ISO C++ standard library. To support this another set of
  221. the functions using locale data exist which have an additional
  222. argument.
  223. Attention: all these functions are *not* standardized in any form.
  224. This is a proof-of-concept implementation. */
  225. /* Structure for reentrant locale using functions. This is an
  226. (almost) opaque type for the user level programs. */
  227. # include <xlocale.h>
  228. /* These definitions are similar to the ones above but all functions
  229. take as an argument a handle for the locale which shall be used. */
  230. # define __isctype_l(c, type, locale) \
  231. ((locale)->__ctype_b[(int) (c)] & (__ctype_mask_t) type)
  232. # define __exctype_l(name) \
  233. extern int name (int, __locale_t) __THROW
  234. /* The following names are all functions:
  235. int isCHARACTERISTIC(int c, locale_t *locale);
  236. which return nonzero iff C has CHARACTERISTIC.
  237. For the meaning of the characteristic names, see the `enum' above. */
  238. __exctype_l (isalnum_l);
  239. __exctype_l (isalpha_l);
  240. __exctype_l (iscntrl_l);
  241. __exctype_l (isdigit_l);
  242. __exctype_l (islower_l);
  243. __exctype_l (isgraph_l);
  244. __exctype_l (isprint_l);
  245. __exctype_l (ispunct_l);
  246. __exctype_l (isspace_l);
  247. __exctype_l (isupper_l);
  248. __exctype_l (isxdigit_l);
  249. __exctype_l (isblank_l);
  250. /* Return the lowercase version of C in locale L. */
  251. extern int __tolower_l (int __c, __locale_t __l) __THROW;
  252. extern int tolower_l (int __c, __locale_t __l) __THROW;
  253. /* Return the uppercase version of C. */
  254. extern int __toupper_l (int __c, __locale_t __l) __THROW;
  255. extern int toupper_l (int __c, __locale_t __l) __THROW;
  256. # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
  257. # define __tolower_l(c, locale) \
  258. __tobody (c, __tolower_l, (locale)->__ctype_tolower, (c, locale))
  259. # define __toupper_l(c, locale) \
  260. __tobody (c, __toupper_l, (locale)->__ctype_toupper, (c, locale))
  261. # define tolower_l(c, locale) __tolower_l ((c), (locale))
  262. # define toupper_l(c, locale) __toupper_l ((c), (locale))
  263. # endif /* Optimizing gcc */
  264. # ifndef __NO_CTYPE
  265. # define __isalnum_l(c,l) __isctype_l((c), _ISalnum, (l))
  266. # define __isalpha_l(c,l) __isctype_l((c), _ISalpha, (l))
  267. # define __iscntrl_l(c,l) __isctype_l((c), _IScntrl, (l))
  268. # define __isdigit_l(c,l) __isctype_l((c), _ISdigit, (l))
  269. # define __islower_l(c,l) __isctype_l((c), _ISlower, (l))
  270. # define __isgraph_l(c,l) __isctype_l((c), _ISgraph, (l))
  271. # define __isprint_l(c,l) __isctype_l((c), _ISprint, (l))
  272. # define __ispunct_l(c,l) __isctype_l((c), _ISpunct, (l))
  273. # define __isspace_l(c,l) __isctype_l((c), _ISspace, (l))
  274. # define __isupper_l(c,l) __isctype_l((c), _ISupper, (l))
  275. # define __isxdigit_l(c,l) __isctype_l((c), _ISxdigit, (l))
  276. # define __isblank_l(c,l) __isctype_l((c), _ISblank, (l))
  277. # if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  278. # define __isascii_l(c,l) ((l), __isascii (c))
  279. # define __toascii_l(c,l) ((l), __toascii (c))
  280. # endif
  281. # define isalnum_l(c,l) __isalnum_l ((c), (l))
  282. # define isalpha_l(c,l) __isalpha_l ((c), (l))
  283. # define iscntrl_l(c,l) __iscntrl_l ((c), (l))
  284. # define isdigit_l(c,l) __isdigit_l ((c), (l))
  285. # define islower_l(c,l) __islower_l ((c), (l))
  286. # define isgraph_l(c,l) __isgraph_l ((c), (l))
  287. # define isprint_l(c,l) __isprint_l ((c), (l))
  288. # define ispunct_l(c,l) __ispunct_l ((c), (l))
  289. # define isspace_l(c,l) __isspace_l ((c), (l))
  290. # define isupper_l(c,l) __isupper_l ((c), (l))
  291. # define isxdigit_l(c,l) __isxdigit_l ((c), (l))
  292. # define isblank_l(c,l) __isblank_l ((c), (l))
  293. # if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  294. # define isascii_l(c,l) __isascii_l ((c), (l))
  295. # define toascii_l(c,l) __toascii_l ((c), (l))
  296. # endif
  297. # endif /* Not __NO_CTYPE. */
  298. #endif /* Use GNU. */
  299. __END_DECLS
  300. #else /* __UCLIBC_HAS_CTYPE_TABLES__ */
  301. #include <bits/uClibc_ctype.h>
  302. #endif
  303. #endif /* ctype.h */