ctype.h 15 KB

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