c-ctype.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* Character handling in C locale.
  2. These functions work like the corresponding functions in <ctype.h>,
  3. except that they have the C (POSIX) locale hardwired, whereas the
  4. <ctype.h> functions' behaviour depends on the current locale set via
  5. setlocale.
  6. Copyright (C) 2000-2003, 2006, 2008 Free Software Foundation, Inc.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software Foundation,
  17. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  18. #ifndef C_CTYPE_H
  19. #define C_CTYPE_H
  20. #include <stdbool.h>
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* The functions defined in this file assume the "C" locale and a character
  25. set without diacritics (ASCII-US or EBCDIC-US or something like that).
  26. Even if the "C" locale on a particular system is an extension of the ASCII
  27. character set (like on BeOS, where it is UTF-8, or on AmigaOS, where it
  28. is ISO-8859-1), the functions in this file recognize only the ASCII
  29. characters. */
  30. /* Check whether the ASCII optimizations apply. */
  31. /* ANSI C89 (and ISO C99 5.2.1.3 too) already guarantees that
  32. '0', '1', ..., '9' have consecutive integer values. */
  33. #define C_CTYPE_CONSECUTIVE_DIGITS 1
  34. #if ('A' <= 'Z') \
  35. && ('A' + 1 == 'B') && ('B' + 1 == 'C') && ('C' + 1 == 'D') \
  36. && ('D' + 1 == 'E') && ('E' + 1 == 'F') && ('F' + 1 == 'G') \
  37. && ('G' + 1 == 'H') && ('H' + 1 == 'I') && ('I' + 1 == 'J') \
  38. && ('J' + 1 == 'K') && ('K' + 1 == 'L') && ('L' + 1 == 'M') \
  39. && ('M' + 1 == 'N') && ('N' + 1 == 'O') && ('O' + 1 == 'P') \
  40. && ('P' + 1 == 'Q') && ('Q' + 1 == 'R') && ('R' + 1 == 'S') \
  41. && ('S' + 1 == 'T') && ('T' + 1 == 'U') && ('U' + 1 == 'V') \
  42. && ('V' + 1 == 'W') && ('W' + 1 == 'X') && ('X' + 1 == 'Y') \
  43. && ('Y' + 1 == 'Z')
  44. #define C_CTYPE_CONSECUTIVE_UPPERCASE 1
  45. #endif
  46. #if ('a' <= 'z') \
  47. && ('a' + 1 == 'b') && ('b' + 1 == 'c') && ('c' + 1 == 'd') \
  48. && ('d' + 1 == 'e') && ('e' + 1 == 'f') && ('f' + 1 == 'g') \
  49. && ('g' + 1 == 'h') && ('h' + 1 == 'i') && ('i' + 1 == 'j') \
  50. && ('j' + 1 == 'k') && ('k' + 1 == 'l') && ('l' + 1 == 'm') \
  51. && ('m' + 1 == 'n') && ('n' + 1 == 'o') && ('o' + 1 == 'p') \
  52. && ('p' + 1 == 'q') && ('q' + 1 == 'r') && ('r' + 1 == 's') \
  53. && ('s' + 1 == 't') && ('t' + 1 == 'u') && ('u' + 1 == 'v') \
  54. && ('v' + 1 == 'w') && ('w' + 1 == 'x') && ('x' + 1 == 'y') \
  55. && ('y' + 1 == 'z')
  56. #define C_CTYPE_CONSECUTIVE_LOWERCASE 1
  57. #endif
  58. #if (' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
  59. && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
  60. && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \
  61. && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \
  62. && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \
  63. && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \
  64. && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \
  65. && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \
  66. && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \
  67. && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \
  68. && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \
  69. && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \
  70. && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \
  71. && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \
  72. && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \
  73. && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \
  74. && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \
  75. && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \
  76. && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \
  77. && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \
  78. && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \
  79. && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
  80. && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126)
  81. /* The character set is ASCII or one of its variants or extensions, not EBCDIC.
  82. Testing the value of '\n' and '\r' is not relevant. */
  83. #define C_CTYPE_ASCII 1
  84. #endif
  85. /* Function declarations. */
  86. /* Unlike the functions in <ctype.h>, which require an argument in the range
  87. of the 'unsigned char' type, the functions here operate on values that are
  88. in the 'unsigned char' range or in the 'char' range. In other words,
  89. when you have a 'char' value, you need to cast it before using it as
  90. argument to a <ctype.h> function:
  91. const char *s = ...;
  92. if (isalpha ((unsigned char) *s)) ...
  93. but you don't need to cast it for the functions defined in this file:
  94. const char *s = ...;
  95. if (c_isalpha (*s)) ...
  96. */
  97. extern bool c_isascii (int c); /* not locale dependent */
  98. extern bool c_isalnum (int c);
  99. extern bool c_isalpha (int c);
  100. extern bool c_isblank (int c);
  101. extern bool c_iscntrl (int c);
  102. extern bool c_isdigit (int c);
  103. extern bool c_islower (int c);
  104. extern bool c_isgraph (int c);
  105. extern bool c_isprint (int c);
  106. extern bool c_ispunct (int c);
  107. extern bool c_isspace (int c);
  108. extern bool c_isupper (int c);
  109. extern bool c_isxdigit (int c);
  110. extern int c_tolower (int c);
  111. extern int c_toupper (int c);
  112. #if defined __GNUC__ && defined __OPTIMIZE__ && !defined __OPTIMIZE_SIZE__ && !defined NO_C_CTYPE_MACROS
  113. /* ASCII optimizations. */
  114. #undef c_isascii
  115. #define c_isascii(c) \
  116. ({ int __c = (c); \
  117. (__c >= 0x00 && __c <= 0x7f); \
  118. })
  119. #if C_CTYPE_CONSECUTIVE_DIGITS \
  120. && C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  121. #if C_CTYPE_ASCII
  122. #undef c_isalnum
  123. #define c_isalnum(c) \
  124. ({ int __c = (c); \
  125. ((__c >= '0' && __c <= '9') \
  126. || ((__c & ~0x20) >= 'A' && (__c & ~0x20) <= 'Z')); \
  127. })
  128. #else
  129. #undef c_isalnum
  130. #define c_isalnum(c) \
  131. ({ int __c = (c); \
  132. ((__c >= '0' && __c <= '9') \
  133. || (__c >= 'A' && __c <= 'Z') \
  134. || (__c >= 'a' && __c <= 'z')); \
  135. })
  136. #endif
  137. #endif
  138. #if C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  139. #if C_CTYPE_ASCII
  140. #undef c_isalpha
  141. #define c_isalpha(c) \
  142. ({ int __c = (c); \
  143. ((__c & ~0x20) >= 'A' && (__c & ~0x20) <= 'Z'); \
  144. })
  145. #else
  146. #undef c_isalpha
  147. #define c_isalpha(c) \
  148. ({ int __c = (c); \
  149. ((__c >= 'A' && __c <= 'Z') || (__c >= 'a' && __c <= 'z')); \
  150. })
  151. #endif
  152. #endif
  153. #undef c_isblank
  154. #define c_isblank(c) \
  155. ({ int __c = (c); \
  156. (__c == ' ' || __c == '\t'); \
  157. })
  158. #if C_CTYPE_ASCII
  159. #undef c_iscntrl
  160. #define c_iscntrl(c) \
  161. ({ int __c = (c); \
  162. ((__c & ~0x1f) == 0 || __c == 0x7f); \
  163. })
  164. #endif
  165. #if C_CTYPE_CONSECUTIVE_DIGITS
  166. #undef c_isdigit
  167. #define c_isdigit(c) \
  168. ({ int __c = (c); \
  169. (__c >= '0' && __c <= '9'); \
  170. })
  171. #endif
  172. #if C_CTYPE_CONSECUTIVE_LOWERCASE
  173. #undef c_islower
  174. #define c_islower(c) \
  175. ({ int __c = (c); \
  176. (__c >= 'a' && __c <= 'z'); \
  177. })
  178. #endif
  179. #if C_CTYPE_ASCII
  180. #undef c_isgraph
  181. #define c_isgraph(c) \
  182. ({ int __c = (c); \
  183. (__c >= '!' && __c <= '~'); \
  184. })
  185. #endif
  186. #if C_CTYPE_ASCII
  187. #undef c_isprint
  188. #define c_isprint(c) \
  189. ({ int __c = (c); \
  190. (__c >= ' ' && __c <= '~'); \
  191. })
  192. #endif
  193. #if C_CTYPE_ASCII
  194. #undef c_ispunct
  195. #define c_ispunct(c) \
  196. ({ int _c = (c); \
  197. (c_isgraph (_c) && ! c_isalnum (_c)); \
  198. })
  199. #endif
  200. #undef c_isspace
  201. #define c_isspace(c) \
  202. ({ int __c = (c); \
  203. (__c == ' ' || __c == '\t' \
  204. || __c == '\n' || __c == '\v' || __c == '\f' || __c == '\r'); \
  205. })
  206. #if C_CTYPE_CONSECUTIVE_UPPERCASE
  207. #undef c_isupper
  208. #define c_isupper(c) \
  209. ({ int __c = (c); \
  210. (__c >= 'A' && __c <= 'Z'); \
  211. })
  212. #endif
  213. #if C_CTYPE_CONSECUTIVE_DIGITS \
  214. && C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  215. #if C_CTYPE_ASCII
  216. #undef c_isxdigit
  217. #define c_isxdigit(c) \
  218. ({ int __c = (c); \
  219. ((__c >= '0' && __c <= '9') \
  220. || ((__c & ~0x20) >= 'A' && (__c & ~0x20) <= 'F')); \
  221. })
  222. #else
  223. #undef c_isxdigit
  224. #define c_isxdigit(c) \
  225. ({ int __c = (c); \
  226. ((__c >= '0' && __c <= '9') \
  227. || (__c >= 'A' && __c <= 'F') \
  228. || (__c >= 'a' && __c <= 'f')); \
  229. })
  230. #endif
  231. #endif
  232. #if C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  233. #undef c_tolower
  234. #define c_tolower(c) \
  235. ({ int __c = (c); \
  236. (__c >= 'A' && __c <= 'Z' ? __c - 'A' + 'a' : __c); \
  237. })
  238. #undef c_toupper
  239. #define c_toupper(c) \
  240. ({ int __c = (c); \
  241. (__c >= 'a' && __c <= 'z' ? __c - 'a' + 'A' : __c); \
  242. })
  243. #endif
  244. #endif /* optimizing for speed */
  245. #ifdef __cplusplus
  246. }
  247. #endif
  248. #endif /* C_CTYPE_H */