c-ctype.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 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 Lesser General Public License as published by
  9. the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser 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. #ifdef _WIN32
  21. #include "win/stdbool.h"
  22. #else
  23. #include <stdbool.h>
  24. #endif
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /* The functions defined in this file assume the "C" locale and a character
  29. set without diacritics (ASCII-US or EBCDIC-US or something like that).
  30. Even if the "C" locale on a particular system is an extension of the ASCII
  31. character set (like on BeOS, where it is UTF-8, or on AmigaOS, where it
  32. is ISO-8859-1), the functions in this file recognize only the ASCII
  33. characters. */
  34. /* Check whether the ASCII optimizations apply. */
  35. /* ANSI C89 (and ISO C99 5.2.1.3 too) already guarantees that
  36. '0', '1', ..., '9' have consecutive integer values. */
  37. #define C_CTYPE_CONSECUTIVE_DIGITS 1
  38. #if ('A' <= 'Z') \
  39. && ('A' + 1 == 'B') && ('B' + 1 == 'C') && ('C' + 1 == 'D') \
  40. && ('D' + 1 == 'E') && ('E' + 1 == 'F') && ('F' + 1 == 'G') \
  41. && ('G' + 1 == 'H') && ('H' + 1 == 'I') && ('I' + 1 == 'J') \
  42. && ('J' + 1 == 'K') && ('K' + 1 == 'L') && ('L' + 1 == 'M') \
  43. && ('M' + 1 == 'N') && ('N' + 1 == 'O') && ('O' + 1 == 'P') \
  44. && ('P' + 1 == 'Q') && ('Q' + 1 == 'R') && ('R' + 1 == 'S') \
  45. && ('S' + 1 == 'T') && ('T' + 1 == 'U') && ('U' + 1 == 'V') \
  46. && ('V' + 1 == 'W') && ('W' + 1 == 'X') && ('X' + 1 == 'Y') \
  47. && ('Y' + 1 == 'Z')
  48. #define C_CTYPE_CONSECUTIVE_UPPERCASE 1
  49. #endif
  50. #if ('a' <= 'z') \
  51. && ('a' + 1 == 'b') && ('b' + 1 == 'c') && ('c' + 1 == 'd') \
  52. && ('d' + 1 == 'e') && ('e' + 1 == 'f') && ('f' + 1 == 'g') \
  53. && ('g' + 1 == 'h') && ('h' + 1 == 'i') && ('i' + 1 == 'j') \
  54. && ('j' + 1 == 'k') && ('k' + 1 == 'l') && ('l' + 1 == 'm') \
  55. && ('m' + 1 == 'n') && ('n' + 1 == 'o') && ('o' + 1 == 'p') \
  56. && ('p' + 1 == 'q') && ('q' + 1 == 'r') && ('r' + 1 == 's') \
  57. && ('s' + 1 == 't') && ('t' + 1 == 'u') && ('u' + 1 == 'v') \
  58. && ('v' + 1 == 'w') && ('w' + 1 == 'x') && ('x' + 1 == 'y') \
  59. && ('y' + 1 == 'z')
  60. #define C_CTYPE_CONSECUTIVE_LOWERCASE 1
  61. #endif
  62. #if (' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
  63. && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
  64. && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \
  65. && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \
  66. && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \
  67. && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \
  68. && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \
  69. && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \
  70. && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \
  71. && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \
  72. && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \
  73. && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \
  74. && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \
  75. && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \
  76. && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \
  77. && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \
  78. && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \
  79. && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \
  80. && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \
  81. && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \
  82. && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \
  83. && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
  84. && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126)
  85. /* The character set is ASCII or one of its variants or extensions, not EBCDIC.
  86. Testing the value of '\n' and '\r' is not relevant. */
  87. #define C_CTYPE_ASCII 1
  88. #endif
  89. /* Function declarations. */
  90. extern bool c_isascii (int c); /* not locale dependent */
  91. extern bool c_isalnum (int c);
  92. extern bool c_isalpha (int c);
  93. extern bool c_isblank (int c);
  94. extern bool c_iscntrl (int c);
  95. extern bool c_isdigit (int c);
  96. extern bool c_islower (int c);
  97. extern bool c_isgraph (int c);
  98. extern bool c_isprint (int c);
  99. extern bool c_ispunct (int c);
  100. extern bool c_isspace (int c);
  101. extern bool c_isupper (int c);
  102. extern bool c_isxdigit (int c);
  103. extern int c_tolower (int c);
  104. extern int c_toupper (int c);
  105. #if defined __GNUC__ && defined __OPTIMIZE__ && !defined __OPTIMIZE_SIZE__ && !defined NO_C_CTYPE_MACROS
  106. /* ASCII optimizations. */
  107. #undef c_isascii
  108. #define c_isascii(c) \
  109. ({ int __c = (c); \
  110. (__c >= 0x00 && __c <= 0x7f); \
  111. })
  112. #if C_CTYPE_CONSECUTIVE_DIGITS \
  113. && C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  114. #if C_CTYPE_ASCII
  115. #undef c_isalnum
  116. #define c_isalnum(c) \
  117. ({ int __c = (c); \
  118. ((__c >= '0' && __c <= '9') \
  119. || ((__c & ~0x20) >= 'A' && (__c & ~0x20) <= 'Z')); \
  120. })
  121. #else
  122. #undef c_isalnum
  123. #define c_isalnum(c) \
  124. ({ int __c = (c); \
  125. ((__c >= '0' && __c <= '9') \
  126. || (__c >= 'A' && __c <= 'Z') \
  127. || (__c >= 'a' && __c <= 'z')); \
  128. })
  129. #endif
  130. #endif
  131. #if C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  132. #if C_CTYPE_ASCII
  133. #undef c_isalpha
  134. #define c_isalpha(c) \
  135. ({ int __c = (c); \
  136. ((__c & ~0x20) >= 'A' && (__c & ~0x20) <= 'Z'); \
  137. })
  138. #else
  139. #undef c_isalpha
  140. #define c_isalpha(c) \
  141. ({ int __c = (c); \
  142. ((__c >= 'A' && __c <= 'Z') || (__c >= 'a' && __c <= 'z')); \
  143. })
  144. #endif
  145. #endif
  146. #undef c_isblank
  147. #define c_isblank(c) \
  148. ({ int __c = (c); \
  149. (__c == ' ' || __c == '\t'); \
  150. })
  151. #if C_CTYPE_ASCII
  152. #undef c_iscntrl
  153. #define c_iscntrl(c) \
  154. ({ int __c = (c); \
  155. ((__c & ~0x1f) == 0 || __c == 0x7f); \
  156. })
  157. #endif
  158. #if C_CTYPE_CONSECUTIVE_DIGITS
  159. #undef c_isdigit
  160. #define c_isdigit(c) \
  161. ({ int __c = (c); \
  162. (__c >= '0' && __c <= '9'); \
  163. })
  164. #endif
  165. #if C_CTYPE_CONSECUTIVE_LOWERCASE
  166. #undef c_islower
  167. #define c_islower(c) \
  168. ({ int __c = (c); \
  169. (__c >= 'a' && __c <= 'z'); \
  170. })
  171. #endif
  172. #if C_CTYPE_ASCII
  173. #undef c_isgraph
  174. #define c_isgraph(c) \
  175. ({ int __c = (c); \
  176. (__c >= '!' && __c <= '~'); \
  177. })
  178. #endif
  179. #if C_CTYPE_ASCII
  180. #undef c_isprint
  181. #define c_isprint(c) \
  182. ({ int __c = (c); \
  183. (__c >= ' ' && __c <= '~'); \
  184. })
  185. #endif
  186. #if C_CTYPE_ASCII
  187. #undef c_ispunct
  188. #define c_ispunct(c) \
  189. ({ int _c = (c); \
  190. (c_isgraph (_c) && ! c_isalnum (_c)); \
  191. })
  192. #endif
  193. #undef c_isspace
  194. #define c_isspace(c) \
  195. ({ int __c = (c); \
  196. (__c == ' ' || __c == '\t' \
  197. || __c == '\n' || __c == '\v' || __c == '\f' || __c == '\r'); \
  198. })
  199. #if C_CTYPE_CONSECUTIVE_UPPERCASE
  200. #undef c_isupper
  201. #define c_isupper(c) \
  202. ({ int __c = (c); \
  203. (__c >= 'A' && __c <= 'Z'); \
  204. })
  205. #endif
  206. #if C_CTYPE_CONSECUTIVE_DIGITS \
  207. && C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  208. #if C_CTYPE_ASCII
  209. #undef c_isxdigit
  210. #define c_isxdigit(c) \
  211. ({ int __c = (c); \
  212. ((__c >= '0' && __c <= '9') \
  213. || ((__c & ~0x20) >= 'A' && (__c & ~0x20) <= 'F')); \
  214. })
  215. #else
  216. #undef c_isxdigit
  217. #define c_isxdigit(c) \
  218. ({ int __c = (c); \
  219. ((__c >= '0' && __c <= '9') \
  220. || (__c >= 'A' && __c <= 'F') \
  221. || (__c >= 'a' && __c <= 'f')); \
  222. })
  223. #endif
  224. #endif
  225. #if C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  226. #undef c_tolower
  227. #define c_tolower(c) \
  228. ({ int __c = (c); \
  229. (__c >= 'A' && __c <= 'Z' ? __c - 'A' + 'a' : __c); \
  230. })
  231. #undef c_toupper
  232. #define c_toupper(c) \
  233. ({ int __c = (c); \
  234. (__c >= 'a' && __c <= 'z' ? __c - 'a' + 'A' : __c); \
  235. })
  236. #endif
  237. #endif /* optimizing for speed */
  238. #ifdef __cplusplus
  239. }
  240. #endif
  241. #endif /* C_CTYPE_H */