ctype.h 14 KB

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