ctype.h 13 KB

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