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