c-ctype.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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-2023 Free Software Foundation, Inc.
  7. This file is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU Lesser General Public License as
  9. published by the Free Software Foundation; either version 2.1 of the
  10. License, or (at your option) any later version.
  11. This file 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 <https://www.gnu.org/licenses/>. */
  17. #ifndef C_CTYPE_H
  18. #define C_CTYPE_H
  19. #ifndef _GL_INLINE_HEADER_BEGIN
  20. #error "Please include config.h first."
  21. #endif
  22. _GL_INLINE_HEADER_BEGIN
  23. #ifndef C_CTYPE_INLINE
  24. # define C_CTYPE_INLINE _GL_INLINE
  25. #endif
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* The functions defined in this file assume the "C" locale and a character
  30. set without diacritics (ASCII-US or EBCDIC-US or something like that).
  31. Even if the "C" locale on a particular system is an extension of the ASCII
  32. character set (like on BeOS, where it is UTF-8, or on AmigaOS, where it
  33. is ISO-8859-1), the functions in this file recognize only the ASCII
  34. characters. */
  35. #if (' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
  36. && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
  37. && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \
  38. && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \
  39. && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \
  40. && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \
  41. && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \
  42. && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \
  43. && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \
  44. && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \
  45. && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \
  46. && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \
  47. && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \
  48. && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \
  49. && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \
  50. && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \
  51. && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \
  52. && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \
  53. && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \
  54. && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \
  55. && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \
  56. && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
  57. && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126)
  58. /* The character set is ASCII or one of its variants or extensions, not EBCDIC.
  59. Testing the value of '\n' and '\r' is not relevant. */
  60. # define C_CTYPE_ASCII 1
  61. #elif ! (' ' == '\x40' && '0' == '\xf0' \
  62. && 'A' == '\xc1' && 'J' == '\xd1' && 'S' == '\xe2' \
  63. && 'a' == '\x81' && 'j' == '\x91' && 's' == '\xa2')
  64. # error "Only ASCII and EBCDIC are supported"
  65. #endif
  66. #if 'A' < 0
  67. # error "EBCDIC and char is signed -- not supported"
  68. #endif
  69. /* Cases for control characters. */
  70. #define _C_CTYPE_CNTRL \
  71. case '\a': case '\b': case '\f': case '\n': \
  72. case '\r': case '\t': case '\v': \
  73. _C_CTYPE_OTHER_CNTRL
  74. /* ASCII control characters other than those with \-letter escapes. */
  75. #if C_CTYPE_ASCII
  76. # define _C_CTYPE_OTHER_CNTRL \
  77. case '\x00': case '\x01': case '\x02': case '\x03': \
  78. case '\x04': case '\x05': case '\x06': case '\x0e': \
  79. case '\x0f': case '\x10': case '\x11': case '\x12': \
  80. case '\x13': case '\x14': case '\x15': case '\x16': \
  81. case '\x17': case '\x18': case '\x19': case '\x1a': \
  82. case '\x1b': case '\x1c': case '\x1d': case '\x1e': \
  83. case '\x1f': case '\x7f'
  84. #else
  85. /* Use EBCDIC code page 1047's assignments for ASCII control chars;
  86. assume all EBCDIC code pages agree about these assignments. */
  87. # define _C_CTYPE_OTHER_CNTRL \
  88. case '\x00': case '\x01': case '\x02': case '\x03': \
  89. case '\x07': case '\x0e': case '\x0f': case '\x10': \
  90. case '\x11': case '\x12': case '\x13': case '\x18': \
  91. case '\x19': case '\x1c': case '\x1d': case '\x1e': \
  92. case '\x1f': case '\x26': case '\x27': case '\x2d': \
  93. case '\x2e': case '\x32': case '\x37': case '\x3c': \
  94. case '\x3d': case '\x3f'
  95. #endif
  96. /* Cases for lowercase hex letters, and lowercase letters, all offset by N. */
  97. #define _C_CTYPE_LOWER_A_THRU_F_N(N) \
  98. case 'a' + (N): case 'b' + (N): case 'c' + (N): case 'd' + (N): \
  99. case 'e' + (N): case 'f' + (N)
  100. #define _C_CTYPE_LOWER_N(N) \
  101. _C_CTYPE_LOWER_A_THRU_F_N(N): \
  102. case 'g' + (N): case 'h' + (N): case 'i' + (N): case 'j' + (N): \
  103. case 'k' + (N): case 'l' + (N): case 'm' + (N): case 'n' + (N): \
  104. case 'o' + (N): case 'p' + (N): case 'q' + (N): case 'r' + (N): \
  105. case 's' + (N): case 't' + (N): case 'u' + (N): case 'v' + (N): \
  106. case 'w' + (N): case 'x' + (N): case 'y' + (N): case 'z' + (N)
  107. /* Cases for hex letters, digits, lower, punct, and upper. */
  108. #define _C_CTYPE_A_THRU_F \
  109. _C_CTYPE_LOWER_A_THRU_F_N (0): \
  110. _C_CTYPE_LOWER_A_THRU_F_N ('A' - 'a')
  111. #define _C_CTYPE_DIGIT \
  112. case '0': case '1': case '2': case '3': \
  113. case '4': case '5': case '6': case '7': \
  114. case '8': case '9'
  115. #define _C_CTYPE_LOWER _C_CTYPE_LOWER_N (0)
  116. #define _C_CTYPE_PUNCT \
  117. case '!': case '"': case '#': case '$': \
  118. case '%': case '&': case '\'': case '(': \
  119. case ')': case '*': case '+': case ',': \
  120. case '-': case '.': case '/': case ':': \
  121. case ';': case '<': case '=': case '>': \
  122. case '?': case '@': case '[': case '\\': \
  123. case ']': case '^': case '_': case '`': \
  124. case '{': case '|': case '}': case '~'
  125. #define _C_CTYPE_UPPER _C_CTYPE_LOWER_N ('A' - 'a')
  126. /* Function definitions. */
  127. /* Unlike the functions in <ctype.h>, which require an argument in the range
  128. of the 'unsigned char' type, the functions here operate on values that are
  129. in the 'unsigned char' range or in the 'char' range. In other words,
  130. when you have a 'char' value, you need to cast it before using it as
  131. argument to a <ctype.h> function:
  132. const char *s = ...;
  133. if (isalpha ((unsigned char) *s)) ...
  134. but you don't need to cast it for the functions defined in this file:
  135. const char *s = ...;
  136. if (c_isalpha (*s)) ...
  137. */
  138. C_CTYPE_INLINE bool
  139. c_isalnum (int c)
  140. {
  141. switch (c)
  142. {
  143. _C_CTYPE_DIGIT:
  144. _C_CTYPE_LOWER:
  145. _C_CTYPE_UPPER:
  146. return true;
  147. default:
  148. return false;
  149. }
  150. }
  151. C_CTYPE_INLINE bool
  152. c_isalpha (int c)
  153. {
  154. switch (c)
  155. {
  156. _C_CTYPE_LOWER:
  157. _C_CTYPE_UPPER:
  158. return true;
  159. default:
  160. return false;
  161. }
  162. }
  163. /* The function isascii is not locale dependent.
  164. Its use in EBCDIC is questionable. */
  165. C_CTYPE_INLINE bool
  166. c_isascii (int c)
  167. {
  168. switch (c)
  169. {
  170. case ' ':
  171. _C_CTYPE_CNTRL:
  172. _C_CTYPE_DIGIT:
  173. _C_CTYPE_LOWER:
  174. _C_CTYPE_PUNCT:
  175. _C_CTYPE_UPPER:
  176. return true;
  177. default:
  178. return false;
  179. }
  180. }
  181. C_CTYPE_INLINE bool
  182. c_isblank (int c)
  183. {
  184. return c == ' ' || c == '\t';
  185. }
  186. C_CTYPE_INLINE bool
  187. c_iscntrl (int c)
  188. {
  189. switch (c)
  190. {
  191. _C_CTYPE_CNTRL:
  192. return true;
  193. default:
  194. return false;
  195. }
  196. }
  197. C_CTYPE_INLINE bool
  198. c_isdigit (int c)
  199. {
  200. switch (c)
  201. {
  202. _C_CTYPE_DIGIT:
  203. return true;
  204. default:
  205. return false;
  206. }
  207. }
  208. C_CTYPE_INLINE bool
  209. c_isgraph (int c)
  210. {
  211. switch (c)
  212. {
  213. _C_CTYPE_DIGIT:
  214. _C_CTYPE_LOWER:
  215. _C_CTYPE_PUNCT:
  216. _C_CTYPE_UPPER:
  217. return true;
  218. default:
  219. return false;
  220. }
  221. }
  222. C_CTYPE_INLINE bool
  223. c_islower (int c)
  224. {
  225. switch (c)
  226. {
  227. _C_CTYPE_LOWER:
  228. return true;
  229. default:
  230. return false;
  231. }
  232. }
  233. C_CTYPE_INLINE bool
  234. c_isprint (int c)
  235. {
  236. switch (c)
  237. {
  238. case ' ':
  239. _C_CTYPE_DIGIT:
  240. _C_CTYPE_LOWER:
  241. _C_CTYPE_PUNCT:
  242. _C_CTYPE_UPPER:
  243. return true;
  244. default:
  245. return false;
  246. }
  247. }
  248. C_CTYPE_INLINE bool
  249. c_ispunct (int c)
  250. {
  251. switch (c)
  252. {
  253. _C_CTYPE_PUNCT:
  254. return true;
  255. default:
  256. return false;
  257. }
  258. }
  259. C_CTYPE_INLINE bool
  260. c_isspace (int c)
  261. {
  262. switch (c)
  263. {
  264. case ' ': case '\t': case '\n': case '\v': case '\f': case '\r':
  265. return true;
  266. default:
  267. return false;
  268. }
  269. }
  270. C_CTYPE_INLINE bool
  271. c_isupper (int c)
  272. {
  273. switch (c)
  274. {
  275. _C_CTYPE_UPPER:
  276. return true;
  277. default:
  278. return false;
  279. }
  280. }
  281. C_CTYPE_INLINE bool
  282. c_isxdigit (int c)
  283. {
  284. switch (c)
  285. {
  286. _C_CTYPE_DIGIT:
  287. _C_CTYPE_A_THRU_F:
  288. return true;
  289. default:
  290. return false;
  291. }
  292. }
  293. C_CTYPE_INLINE int
  294. c_tolower (int c)
  295. {
  296. switch (c)
  297. {
  298. _C_CTYPE_UPPER:
  299. return c - 'A' + 'a';
  300. default:
  301. return c;
  302. }
  303. }
  304. C_CTYPE_INLINE int
  305. c_toupper (int c)
  306. {
  307. switch (c)
  308. {
  309. _C_CTYPE_LOWER:
  310. return c - 'a' + 'A';
  311. default:
  312. return c;
  313. }
  314. }
  315. #ifdef __cplusplus
  316. }
  317. #endif
  318. _GL_INLINE_HEADER_END
  319. #endif /* C_CTYPE_H */