ctype.h 13 KB

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