ctype.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #ifndef __UCLIBC_HAS_CTYPE_TABLES__
  24. /* "Stub locale": we are permanently in C/POSIX locale.
  25. * Using simple(r) ctype.h machinery in this header instead: */
  26. #include <bits/uClibc_ctype.h>
  27. #else
  28. __BEGIN_DECLS
  29. /* These are all the characteristics of characters.
  30. If there get to be more than 16 distinct characteristics,
  31. __ctype_mask_t will need to be adjusted. */
  32. enum
  33. {
  34. _ISupper = 1 << 0, /* UPPERCASE. */
  35. _ISlower = 1 << 1, /* lowercase. */
  36. _ISalpha = 1 << 2, /* Alphabetic. */
  37. _ISdigit = 1 << 3, /* Numeric. */
  38. _ISxdigit = 1 << 4, /* Hexadecimal numeric. */
  39. _ISspace = 1 << 5, /* Whitespace. */
  40. _ISprint = 1 << 6, /* Printing. */
  41. _ISgraph = 1 << 7, /* Graphical. */
  42. _ISblank = 1 << 8, /* Blank (usually SPC and TAB). */
  43. _IScntrl = 1 << 9, /* Control character. */
  44. _ISpunct = 1 << 10, /* Punctuation. */
  45. _ISalnum = 1 << 11, /* Alphanumeric. */
  46. };
  47. /* __ctype_XXX_t types and __UCLIBC_CTYPE_x_TBL_OFFSET constants */
  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
  52. # define __UCLIBC_CTYPE_IN_TO_DOMAIN(c) (((unsigned int)(c)) < 256)
  53. #endif
  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. libc_hidden_proto(__C_ctype_b)
  86. extern const __ctype_touplow_t *__C_ctype_toupper;
  87. libc_hidden_proto(__C_ctype_toupper)
  88. extern const __ctype_touplow_t *__C_ctype_tolower;
  89. libc_hidden_proto(__C_ctype_tolower)
  90. #ifdef __UCLIBC_HAS_XLOCALE__
  91. const __ctype_mask_t **__ctype_b_loc(void) __attribute__ ((const));
  92. libc_hidden_proto(__ctype_b_loc)
  93. const __ctype_touplow_t **__ctype_tolower_loc(void) __attribute__ ((const));
  94. libc_hidden_proto(__ctype_tolower_loc)
  95. const __ctype_touplow_t **__ctype_toupper_loc(void) __attribute__ ((const));
  96. libc_hidden_proto(__ctype_toupper_loc)
  97. #define __UCLIBC_CTYPE_B (*__ctype_b_loc())
  98. #define __UCLIBC_CTYPE_TOLOWER (*__ctype_tolower_loc())
  99. #define __UCLIBC_CTYPE_TOUPPER (*__ctype_toupper_loc())
  100. #else /* __UCLIBC_HAS_XLOCALE__ */
  101. /* Pointers to the current global locale data in use. */
  102. extern const __ctype_mask_t *__ctype_b;
  103. libc_hidden_proto(__ctype_b)
  104. extern const __ctype_touplow_t *__ctype_toupper;
  105. libc_hidden_proto(__ctype_toupper)
  106. extern const __ctype_touplow_t *__ctype_tolower;
  107. libc_hidden_proto(__ctype_tolower)
  108. #define __UCLIBC_CTYPE_B (__ctype_b)
  109. #define __UCLIBC_CTYPE_TOLOWER (__ctype_tolower)
  110. #define __UCLIBC_CTYPE_TOUPPER (__ctype_toupper)
  111. #endif /* __UCLIBC_HAS_XLOCALE__ */
  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. /* 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. __BEGIN_NAMESPACE_STD
  120. /* The following names are all functions:
  121. int isCHARACTERISTIC(int c);
  122. which return nonzero iff C has CHARACTERISTIC.
  123. For the meaning of the characteristic names, see the `enum' above. */
  124. extern int isalnum(int __c) __THROW;
  125. libc_hidden_proto(isalnum)
  126. extern int isalpha(int __c) __THROW;
  127. libc_hidden_proto(isalpha)
  128. extern int iscntrl(int __c) __THROW;
  129. libc_hidden_proto(iscntrl)
  130. extern int isdigit(int __c) __THROW;
  131. libc_hidden_proto(isdigit)
  132. extern int islower(int __c) __THROW;
  133. libc_hidden_proto(islower)
  134. extern int isgraph(int __c) __THROW;
  135. libc_hidden_proto(isgraph)
  136. extern int isprint(int __c) __THROW;
  137. libc_hidden_proto(isprint)
  138. extern int ispunct(int __c) __THROW;
  139. libc_hidden_proto(ispunct)
  140. extern int isspace(int __c) __THROW;
  141. libc_hidden_proto(isspace)
  142. extern int isupper(int __c) __THROW;
  143. libc_hidden_proto(isupper)
  144. extern int isxdigit(int __c) __THROW;
  145. libc_hidden_proto(isxdigit)
  146. /* Return the lowercase version of C. */
  147. extern int tolower(int __c) __THROW;
  148. libc_hidden_proto(tolower)
  149. /* Return the uppercase version of C. */
  150. extern int toupper(int __c) __THROW;
  151. libc_hidden_proto(toupper)
  152. __END_NAMESPACE_STD
  153. /* ISO C99 introduced one new function. */
  154. #ifdef __USE_ISOC99
  155. __BEGIN_NAMESPACE_C99
  156. extern int isblank(int __c) __THROW;
  157. libc_hidden_proto(isblank)
  158. __END_NAMESPACE_C99
  159. #endif
  160. #ifdef __USE_GNU
  161. /* Test C for a set of character classes according to MASK. */
  162. extern int isctype(int __c, int __mask) __THROW;
  163. #endif
  164. #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  165. /* Return nonzero iff C is in the ASCII set
  166. (i.e., is no more than 7 bits wide). */
  167. extern int isascii(int __c) __THROW;
  168. libc_hidden_proto(isascii)
  169. /* Return the part of C that is in the ASCII set
  170. (i.e., the low-order 7 bits of C). */
  171. extern int toascii(int __c) __THROW;
  172. /* These are the same as `toupper' and `tolower' except that they do not
  173. check the argument for being in the range of a `char'. */
  174. extern int _toupper(int __c) __THROW;
  175. extern int _tolower(int __c) __THROW;
  176. #endif /* Use SVID or use misc. */
  177. /* This code is needed for the optimized mapping functions. */
  178. #define __tobody(c, f, table, args) \
  179. (__extension__ ({ \
  180. int __res; \
  181. if (sizeof(c) > 1) { \
  182. if (__builtin_constant_p(c)) { \
  183. int __c = (c); \
  184. __res = __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (table)[__c] : __c; \
  185. } else \
  186. __res = f args; \
  187. } else \
  188. __res = (table)[(int) (c)]; \
  189. __res; \
  190. }))
  191. #define __isctype(c, type) ((__UCLIBC_CTYPE_B)[(int)(c)] & (__ctype_mask_t)type)
  192. #if !defined __NO_CTYPE && !defined __cplusplus
  193. # define isalnum(c) __isctype((c), _ISalnum)
  194. # define isalpha(c) __isctype((c), _ISalpha)
  195. # define iscntrl(c) __isctype((c), _IScntrl)
  196. # define isdigit(c) __isctype((c), _ISdigit)
  197. # define islower(c) __isctype((c), _ISlower)
  198. # define isgraph(c) __isctype((c), _ISgraph)
  199. # define isprint(c) __isctype((c), _ISprint)
  200. # define ispunct(c) __isctype((c), _ISpunct)
  201. # define isspace(c) __isctype((c), _ISspace)
  202. # define isupper(c) __isctype((c), _ISupper)
  203. # define isxdigit(c) __isctype((c), _ISxdigit)
  204. # ifdef __USE_ISOC99
  205. # define isblank(c) __isctype((c), _ISblank)
  206. # endif
  207. # ifdef __USE_EXTERN_INLINES
  208. __extern_inline int
  209. __NTH (tolower (int __c))
  210. {
  211. return __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (__UCLIBC_CTYPE_TOLOWER)[__c] : __c;
  212. }
  213. __extern_inline int
  214. __NTH (toupper (int __c))
  215. {
  216. return __UCLIBC_CTYPE_IN_TO_DOMAIN(__c) ? (__UCLIBC_CTYPE_TOUPPER)[__c] : __c;
  217. }
  218. # endif
  219. # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
  220. # define tolower(c) __tobody(c, tolower, __UCLIBC_CTYPE_TOLOWER, (c))
  221. # define toupper(c) __tobody(c, toupper, __UCLIBC_CTYPE_TOUPPER, (c))
  222. # endif /* Optimizing gcc */
  223. # if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  224. # define isascii(c) __isascii (c)
  225. # define toascii(c) __toascii (c)
  226. # define _tolower(c) ((int) (__UCLIBC_CTYPE_TOLOWER)[(int) (c)])
  227. # define _toupper(c) ((int) (__UCLIBC_CTYPE_TOUPPER)[(int) (c)])
  228. # endif
  229. #endif /* Not __NO_CTYPE. */
  230. #if defined __USE_GNU && defined __UCLIBC_HAS_XLOCALE__
  231. /* The concept of one static locale per category is not very well
  232. thought out. Many applications will need to process its data using
  233. information from several different locales. Another application is
  234. the implementation of the internationalization handling in the
  235. upcoming ISO C++ standard library. To support this another set of
  236. the functions using locale data exist which have an additional
  237. argument.
  238. Attention: all these functions are *not* standardized in any form.
  239. This is a proof-of-concept implementation. */
  240. /* Structure for reentrant locale using functions. This is an
  241. (almost) opaque type for the user level programs. */
  242. # include <xlocale.h>
  243. /* These definitions are similar to the ones above but all functions
  244. take as an argument a handle for the locale which shall be used. */
  245. extern int isalnum_l(int, __locale_t) __THROW;
  246. libc_hidden_proto(isalnum_l)
  247. extern int isalpha_l(int, __locale_t) __THROW;
  248. libc_hidden_proto(isalpha_l)
  249. extern int iscntrl_l(int, __locale_t) __THROW;
  250. libc_hidden_proto(iscntrl_l)
  251. extern int isdigit_l(int, __locale_t) __THROW;
  252. libc_hidden_proto(isdigit_l)
  253. extern int islower_l(int, __locale_t) __THROW;
  254. libc_hidden_proto(islower_l)
  255. extern int isgraph_l(int, __locale_t) __THROW;
  256. libc_hidden_proto(isgraph_l)
  257. extern int isprint_l(int, __locale_t) __THROW;
  258. libc_hidden_proto(isprint_l)
  259. extern int ispunct_l(int, __locale_t) __THROW;
  260. libc_hidden_proto(ispunct_l)
  261. extern int isspace_l(int, __locale_t) __THROW;
  262. libc_hidden_proto(isspace_l)
  263. extern int isupper_l(int, __locale_t) __THROW;
  264. libc_hidden_proto(isupper_l)
  265. extern int isxdigit_l(int, __locale_t) __THROW;
  266. libc_hidden_proto(isxdigit_l)
  267. extern int isblank_l(int, __locale_t) __THROW;
  268. libc_hidden_proto(isblank_l)
  269. # if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  270. /* Return nonzero iff C is in the ASCII set
  271. (i.e., is no more than 7 bits wide). */
  272. extern int isascii_l (int __c) __THROW;
  273. libc_hidden_proto(isascii_l)
  274. # endif
  275. /* Return the lowercase version of C in locale L. */
  276. // "ordinary" ctype.h has no __tolower, why we try to have it?
  277. // remove after 0.9.31
  278. //extern int __tolower_l (int __c, __locale_t __l) __THROW;
  279. extern int tolower_l (int __c, __locale_t __l) __THROW;
  280. libc_hidden_proto(tolower_l)
  281. /* Return the uppercase version of C. */
  282. //extern int __toupper_l (int __c, __locale_t __l) __THROW;
  283. extern int toupper_l (int __c, __locale_t __l) __THROW;
  284. libc_hidden_proto(toupper_l)
  285. # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
  286. # define tolower_l(c, locale) __tobody(c, tolower_l, (locale)->__ctype_tolower, (c, locale))
  287. # define toupper_l(c, locale) __tobody(c, toupper_l, (locale)->__ctype_toupper, (c, locale))
  288. //# define __tolower_l(c, locale) tolower_l((c), (locale))
  289. //# define __toupper_l(c, locale) toupper_l((c), (locale))
  290. # endif /* Optimizing gcc */
  291. # define __isctype_l(c, type, locale) ((locale)->__ctype_b[(int) (c)] & (__ctype_mask_t) type)
  292. # ifndef __NO_CTYPE
  293. # define __isalnum_l(c,l) __isctype_l((c), _ISalnum, (l))
  294. # define __isalpha_l(c,l) __isctype_l((c), _ISalpha, (l))
  295. # define __iscntrl_l(c,l) __isctype_l((c), _IScntrl, (l))
  296. # define __isdigit_l(c,l) __isctype_l((c), _ISdigit, (l))
  297. # define __islower_l(c,l) __isctype_l((c), _ISlower, (l))
  298. # define __isgraph_l(c,l) __isctype_l((c), _ISgraph, (l))
  299. # define __isprint_l(c,l) __isctype_l((c), _ISprint, (l))
  300. # define __ispunct_l(c,l) __isctype_l((c), _ISpunct, (l))
  301. # define __isspace_l(c,l) __isctype_l((c), _ISspace, (l))
  302. # define __isupper_l(c,l) __isctype_l((c), _ISupper, (l))
  303. # define __isxdigit_l(c,l) __isctype_l((c), _ISxdigit, (l))
  304. # define __isblank_l(c,l) __isctype_l((c), _ISblank, (l))
  305. # if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  306. # define __isascii_l(c,l) ((l), __isascii (c))
  307. # define __toascii_l(c,l) ((l), __toascii (c))
  308. # endif
  309. # define isalnum_l(c,l) __isalnum_l ((c), (l))
  310. # define isalpha_l(c,l) __isalpha_l ((c), (l))
  311. # define iscntrl_l(c,l) __iscntrl_l ((c), (l))
  312. # define isdigit_l(c,l) __isdigit_l ((c), (l))
  313. # define islower_l(c,l) __islower_l ((c), (l))
  314. # define isgraph_l(c,l) __isgraph_l ((c), (l))
  315. # define isprint_l(c,l) __isprint_l ((c), (l))
  316. # define ispunct_l(c,l) __ispunct_l ((c), (l))
  317. # define isspace_l(c,l) __isspace_l ((c), (l))
  318. # define isupper_l(c,l) __isupper_l ((c), (l))
  319. # define isxdigit_l(c,l) __isxdigit_l ((c), (l))
  320. # define isblank_l(c,l) __isblank_l ((c), (l))
  321. # if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
  322. # define isascii_l(c,l) __isascii_l ((c), (l))
  323. # define toascii_l(c,l) __toascii_l ((c), (l))
  324. # endif
  325. # endif /* Not __NO_CTYPE. */
  326. #endif /* Use GNU. */
  327. __END_DECLS
  328. #endif /* __UCLIBC_HAS_CTYPE_TABLES__ */
  329. #endif /* ctype.h */