c-ctype.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* Character handling in C locale.
  2. Copyright 2000-2003, 2006 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #include <config.h>
  15. /* Specification. */
  16. #define NO_C_CTYPE_MACROS
  17. #include "c-ctype.h"
  18. /* The function isascii is not locale dependent. Its use in EBCDIC is
  19. questionable. */
  20. bool
  21. c_isascii (int c)
  22. {
  23. return (c >= 0x00 && c <= 0x7f);
  24. }
  25. bool
  26. c_isalnum (int c)
  27. {
  28. #if C_CTYPE_CONSECUTIVE_DIGITS \
  29. && C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  30. #if C_CTYPE_ASCII
  31. return ((c >= '0' && c <= '9')
  32. || ((c & ~0x20) >= 'A' && (c & ~0x20) <= 'Z'));
  33. #else
  34. return ((c >= '0' && c <= '9')
  35. || (c >= 'A' && c <= 'Z')
  36. || (c >= 'a' && c <= 'z'));
  37. #endif
  38. #else
  39. switch (c)
  40. {
  41. case '0': case '1': case '2': case '3': case '4': case '5':
  42. case '6': case '7': case '8': case '9':
  43. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  44. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  45. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  46. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  47. case 'Y': case 'Z':
  48. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  49. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  50. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  51. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  52. case 'y': case 'z':
  53. return 1;
  54. default:
  55. return 0;
  56. }
  57. #endif
  58. }
  59. bool
  60. c_isalpha (int c)
  61. {
  62. #if C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  63. #if C_CTYPE_ASCII
  64. return ((c & ~0x20) >= 'A' && (c & ~0x20) <= 'Z');
  65. #else
  66. return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
  67. #endif
  68. #else
  69. switch (c)
  70. {
  71. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  72. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  73. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  74. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  75. case 'Y': case 'Z':
  76. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  77. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  78. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  79. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  80. case 'y': case 'z':
  81. return 1;
  82. default:
  83. return 0;
  84. }
  85. #endif
  86. }
  87. bool
  88. c_isblank (int c)
  89. {
  90. return (c == ' ' || c == '\t');
  91. }
  92. bool
  93. c_iscntrl (int c)
  94. {
  95. #if C_CTYPE_ASCII
  96. return ((c & ~0x1f) == 0 || c == 0x7f);
  97. #else
  98. switch (c)
  99. {
  100. case ' ': case '!': case '"': case '#': case '$': case '%':
  101. case '&': case '\'': case '(': case ')': case '*': case '+':
  102. case ',': case '-': case '.': case '/':
  103. case '0': case '1': case '2': case '3': case '4': case '5':
  104. case '6': case '7': case '8': case '9':
  105. case ':': case ';': case '<': case '=': case '>': case '?':
  106. case '@':
  107. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  108. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  109. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  110. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  111. case 'Y': case 'Z':
  112. case '[': case '\\': case ']': case '^': case '_': case '`':
  113. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  114. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  115. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  116. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  117. case 'y': case 'z':
  118. case '{': case '|': case '}': case '~':
  119. return 0;
  120. default:
  121. return 1;
  122. }
  123. #endif
  124. }
  125. bool
  126. c_isdigit (int c)
  127. {
  128. #if C_CTYPE_CONSECUTIVE_DIGITS
  129. return (c >= '0' && c <= '9');
  130. #else
  131. switch (c)
  132. {
  133. case '0': case '1': case '2': case '3': case '4': case '5':
  134. case '6': case '7': case '8': case '9':
  135. return 1;
  136. default:
  137. return 0;
  138. }
  139. #endif
  140. }
  141. bool
  142. c_islower (int c)
  143. {
  144. #if C_CTYPE_CONSECUTIVE_LOWERCASE
  145. return (c >= 'a' && c <= 'z');
  146. #else
  147. switch (c)
  148. {
  149. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  150. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  151. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  152. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  153. case 'y': case 'z':
  154. return 1;
  155. default:
  156. return 0;
  157. }
  158. #endif
  159. }
  160. bool
  161. c_isgraph (int c)
  162. {
  163. #if C_CTYPE_ASCII
  164. return (c >= '!' && c <= '~');
  165. #else
  166. switch (c)
  167. {
  168. case '!': case '"': case '#': case '$': case '%': case '&':
  169. case '\'': case '(': case ')': case '*': case '+': case ',':
  170. case '-': case '.': case '/':
  171. case '0': case '1': case '2': case '3': case '4': case '5':
  172. case '6': case '7': case '8': case '9':
  173. case ':': case ';': case '<': case '=': case '>': case '?':
  174. case '@':
  175. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  176. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  177. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  178. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  179. case 'Y': case 'Z':
  180. case '[': case '\\': case ']': case '^': case '_': case '`':
  181. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  182. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  183. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  184. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  185. case 'y': case 'z':
  186. case '{': case '|': case '}': case '~':
  187. return 1;
  188. default:
  189. return 0;
  190. }
  191. #endif
  192. }
  193. bool
  194. c_isprint (int c)
  195. {
  196. #if C_CTYPE_ASCII
  197. return (c >= ' ' && c <= '~');
  198. #else
  199. switch (c)
  200. {
  201. case ' ': case '!': case '"': case '#': case '$': case '%':
  202. case '&': case '\'': case '(': case ')': case '*': case '+':
  203. case ',': case '-': case '.': case '/':
  204. case '0': case '1': case '2': case '3': case '4': case '5':
  205. case '6': case '7': case '8': case '9':
  206. case ':': case ';': case '<': case '=': case '>': case '?':
  207. case '@':
  208. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  209. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  210. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  211. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  212. case 'Y': case 'Z':
  213. case '[': case '\\': case ']': case '^': case '_': case '`':
  214. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  215. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  216. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  217. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  218. case 'y': case 'z':
  219. case '{': case '|': case '}': case '~':
  220. return 1;
  221. default:
  222. return 0;
  223. }
  224. #endif
  225. }
  226. bool
  227. c_ispunct (int c)
  228. {
  229. #if C_CTYPE_ASCII
  230. return ((c >= '!' && c <= '~')
  231. && !((c >= '0' && c <= '9')
  232. || ((c & ~0x20) >= 'A' && (c & ~0x20) <= 'Z')));
  233. #else
  234. switch (c)
  235. {
  236. case '!': case '"': case '#': case '$': case '%': case '&':
  237. case '\'': case '(': case ')': case '*': case '+': case ',':
  238. case '-': case '.': case '/':
  239. case ':': case ';': case '<': case '=': case '>': case '?':
  240. case '@':
  241. case '[': case '\\': case ']': case '^': case '_': case '`':
  242. case '{': case '|': case '}': case '~':
  243. return 1;
  244. default:
  245. return 0;
  246. }
  247. #endif
  248. }
  249. bool
  250. c_isspace (int c)
  251. {
  252. return (c == ' ' || c == '\t'
  253. || c == '\n' || c == '\v' || c == '\f' || c == '\r');
  254. }
  255. bool
  256. c_isupper (int c)
  257. {
  258. #if C_CTYPE_CONSECUTIVE_UPPERCASE
  259. return (c >= 'A' && c <= 'Z');
  260. #else
  261. switch (c)
  262. {
  263. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  264. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  265. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  266. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  267. case 'Y': case 'Z':
  268. return 1;
  269. default:
  270. return 0;
  271. }
  272. #endif
  273. }
  274. bool
  275. c_isxdigit (int c)
  276. {
  277. #if C_CTYPE_CONSECUTIVE_DIGITS \
  278. && C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  279. #if C_CTYPE_ASCII
  280. return ((c >= '0' && c <= '9')
  281. || ((c & ~0x20) >= 'A' && (c & ~0x20) <= 'F'));
  282. #else
  283. return ((c >= '0' && c <= '9')
  284. || (c >= 'A' && c <= 'F')
  285. || (c >= 'a' && c <= 'f'));
  286. #endif
  287. #else
  288. switch (c)
  289. {
  290. case '0': case '1': case '2': case '3': case '4': case '5':
  291. case '6': case '7': case '8': case '9':
  292. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  293. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  294. return 1;
  295. default:
  296. return 0;
  297. }
  298. #endif
  299. }
  300. int
  301. c_tolower (int c)
  302. {
  303. #if C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  304. return (c >= 'A' && c <= 'Z' ? c - 'A' + 'a' : c);
  305. #else
  306. switch (c)
  307. {
  308. case 'A': return 'a';
  309. case 'B': return 'b';
  310. case 'C': return 'c';
  311. case 'D': return 'd';
  312. case 'E': return 'e';
  313. case 'F': return 'f';
  314. case 'G': return 'g';
  315. case 'H': return 'h';
  316. case 'I': return 'i';
  317. case 'J': return 'j';
  318. case 'K': return 'k';
  319. case 'L': return 'l';
  320. case 'M': return 'm';
  321. case 'N': return 'n';
  322. case 'O': return 'o';
  323. case 'P': return 'p';
  324. case 'Q': return 'q';
  325. case 'R': return 'r';
  326. case 'S': return 's';
  327. case 'T': return 't';
  328. case 'U': return 'u';
  329. case 'V': return 'v';
  330. case 'W': return 'w';
  331. case 'X': return 'x';
  332. case 'Y': return 'y';
  333. case 'Z': return 'z';
  334. default: return c;
  335. }
  336. #endif
  337. }
  338. int
  339. c_toupper (int c)
  340. {
  341. #if C_CTYPE_CONSECUTIVE_UPPERCASE && C_CTYPE_CONSECUTIVE_LOWERCASE
  342. return (c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c);
  343. #else
  344. switch (c)
  345. {
  346. case 'a': return 'A';
  347. case 'b': return 'B';
  348. case 'c': return 'C';
  349. case 'd': return 'D';
  350. case 'e': return 'E';
  351. case 'f': return 'F';
  352. case 'g': return 'G';
  353. case 'h': return 'H';
  354. case 'i': return 'I';
  355. case 'j': return 'J';
  356. case 'k': return 'K';
  357. case 'l': return 'L';
  358. case 'm': return 'M';
  359. case 'n': return 'N';
  360. case 'o': return 'O';
  361. case 'p': return 'P';
  362. case 'q': return 'Q';
  363. case 'r': return 'R';
  364. case 's': return 'S';
  365. case 't': return 'T';
  366. case 'u': return 'U';
  367. case 'v': return 'V';
  368. case 'w': return 'W';
  369. case 'x': return 'X';
  370. case 'y': return 'Y';
  371. case 'z': return 'Z';
  372. default: return c;
  373. }
  374. #endif
  375. }