safe-ctype.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* <ctype.h> replacement macros.
  2. Copyright (C) 2000, 2001, 2002, 2003, 2004,
  3. 2005 Free Software Foundation, Inc.
  4. Contributed by Zack Weinberg <zackw@stanford.edu>.
  5. This file is part of the libiberty library.
  6. Libiberty is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. Libiberty is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with libiberty; see the file COPYING.LIB. If
  16. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  17. Boston, MA 02110-1301, USA. */
  18. /*
  19. @defvr Extension HOST_CHARSET
  20. This macro indicates the basic character set and encoding used by the
  21. host: more precisely, the encoding used for character constants in
  22. preprocessor @samp{#if} statements (the C "execution character set").
  23. It is defined by @file{safe-ctype.h}, and will be an integer constant
  24. with one of the following values:
  25. @ftable @code
  26. @item HOST_CHARSET_UNKNOWN
  27. The host character set is unknown - that is, not one of the next two
  28. possibilities.
  29. @item HOST_CHARSET_ASCII
  30. The host character set is ASCII.
  31. @item HOST_CHARSET_EBCDIC
  32. The host character set is some variant of EBCDIC. (Only one of the
  33. nineteen EBCDIC varying characters is tested; exercise caution.)
  34. @end ftable
  35. @end defvr
  36. @deffn Extension ISALPHA (@var{c})
  37. @deffnx Extension ISALNUM (@var{c})
  38. @deffnx Extension ISBLANK (@var{c})
  39. @deffnx Extension ISCNTRL (@var{c})
  40. @deffnx Extension ISDIGIT (@var{c})
  41. @deffnx Extension ISGRAPH (@var{c})
  42. @deffnx Extension ISLOWER (@var{c})
  43. @deffnx Extension ISPRINT (@var{c})
  44. @deffnx Extension ISPUNCT (@var{c})
  45. @deffnx Extension ISSPACE (@var{c})
  46. @deffnx Extension ISUPPER (@var{c})
  47. @deffnx Extension ISXDIGIT (@var{c})
  48. These twelve macros are defined by @file{safe-ctype.h}. Each has the
  49. same meaning as the corresponding macro (with name in lowercase)
  50. defined by the standard header @file{ctype.h}. For example,
  51. @code{ISALPHA} returns true for alphabetic characters and false for
  52. others. However, there are two differences between these macros and
  53. those provided by @file{ctype.h}:
  54. @itemize @bullet
  55. @item These macros are guaranteed to have well-defined behavior for all
  56. values representable by @code{signed char} and @code{unsigned char}, and
  57. for @code{EOF}.
  58. @item These macros ignore the current locale; they are true for these
  59. fixed sets of characters:
  60. @multitable {@code{XDIGIT}} {yada yada yada yada yada yada yada yada}
  61. @item @code{ALPHA} @tab @kbd{A-Za-z}
  62. @item @code{ALNUM} @tab @kbd{A-Za-z0-9}
  63. @item @code{BLANK} @tab @kbd{space tab}
  64. @item @code{CNTRL} @tab @code{!PRINT}
  65. @item @code{DIGIT} @tab @kbd{0-9}
  66. @item @code{GRAPH} @tab @code{ALNUM || PUNCT}
  67. @item @code{LOWER} @tab @kbd{a-z}
  68. @item @code{PRINT} @tab @code{GRAPH ||} @kbd{space}
  69. @item @code{PUNCT} @tab @kbd{`~!@@#$%^&*()_-=+[@{]@}\|;:'",<.>/?}
  70. @item @code{SPACE} @tab @kbd{space tab \n \r \f \v}
  71. @item @code{UPPER} @tab @kbd{A-Z}
  72. @item @code{XDIGIT} @tab @kbd{0-9A-Fa-f}
  73. @end multitable
  74. Note that, if the host character set is ASCII or a superset thereof,
  75. all these macros will return false for all values of @code{char} outside
  76. the range of 7-bit ASCII. In particular, both ISPRINT and ISCNTRL return
  77. false for characters with numeric values from 128 to 255.
  78. @end itemize
  79. @end deffn
  80. @deffn Extension ISIDNUM (@var{c})
  81. @deffnx Extension ISIDST (@var{c})
  82. @deffnx Extension IS_VSPACE (@var{c})
  83. @deffnx Extension IS_NVSPACE (@var{c})
  84. @deffnx Extension IS_SPACE_OR_NUL (@var{c})
  85. @deffnx Extension IS_ISOBASIC (@var{c})
  86. These six macros are defined by @file{safe-ctype.h} and provide
  87. additional character classes which are useful when doing lexical
  88. analysis of C or similar languages. They are true for the following
  89. sets of characters:
  90. @multitable {@code{SPACE_OR_NUL}} {yada yada yada yada yada yada yada yada}
  91. @item @code{IDNUM} @tab @kbd{A-Za-z0-9_}
  92. @item @code{IDST} @tab @kbd{A-Za-z_}
  93. @item @code{VSPACE} @tab @kbd{\r \n}
  94. @item @code{NVSPACE} @tab @kbd{space tab \f \v \0}
  95. @item @code{SPACE_OR_NUL} @tab @code{VSPACE || NVSPACE}
  96. @item @code{ISOBASIC} @tab @code{VSPACE || NVSPACE || PRINT}
  97. @end multitable
  98. @end deffn
  99. */
  100. #include "ansidecl.h"
  101. #include <safe-ctype.h>
  102. #include <stdio.h> /* for EOF */
  103. #if EOF != -1
  104. #error "<safe-ctype.h> requires EOF == -1"
  105. #endif
  106. /* Shorthand */
  107. #define bl _sch_isblank
  108. #define cn _sch_iscntrl
  109. #define di _sch_isdigit
  110. #define is _sch_isidst
  111. #define lo _sch_islower
  112. #define nv _sch_isnvsp
  113. #define pn _sch_ispunct
  114. #define pr _sch_isprint
  115. #define sp _sch_isspace
  116. #define up _sch_isupper
  117. #define vs _sch_isvsp
  118. #define xd _sch_isxdigit
  119. /* Masks. */
  120. #define L (const unsigned short) (lo|is |pr) /* lower case letter */
  121. #define XL (const unsigned short) (lo|is|xd|pr) /* lowercase hex digit */
  122. #define U (const unsigned short) (up|is |pr) /* upper case letter */
  123. #define XU (const unsigned short) (up|is|xd|pr) /* uppercase hex digit */
  124. #define D (const unsigned short) (di |xd|pr) /* decimal digit */
  125. #define P (const unsigned short) (pn |pr) /* punctuation */
  126. #define _ (const unsigned short) (pn|is |pr) /* underscore */
  127. #define C (const unsigned short) ( cn) /* control character */
  128. #define Z (const unsigned short) (nv |cn) /* NUL */
  129. #define M (const unsigned short) (nv|sp |cn) /* cursor movement: \f \v */
  130. #define V (const unsigned short) (vs|sp |cn) /* vertical space: \r \n */
  131. #define T (const unsigned short) (nv|sp|bl|cn) /* tab */
  132. #define S (const unsigned short) (nv|sp|bl|pr) /* space */
  133. /* Are we ASCII? */
  134. #if HOST_CHARSET == HOST_CHARSET_ASCII
  135. const unsigned short _sch_istable[256] =
  136. {
  137. Z, C, C, C, C, C, C, C, /* NUL SOH STX ETX EOT ENQ ACK BEL */
  138. C, T, V, M, M, V, C, C, /* BS HT LF VT FF CR SO SI */
  139. C, C, C, C, C, C, C, C, /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB */
  140. C, C, C, C, C, C, C, C, /* CAN EM SUB ESC FS GS RS US */
  141. S, P, P, P, P, P, P, P, /* SP ! " # $ % & ' */
  142. P, P, P, P, P, P, P, P, /* ( ) * + , - . / */
  143. D, D, D, D, D, D, D, D, /* 0 1 2 3 4 5 6 7 */
  144. D, D, P, P, P, P, P, P, /* 8 9 : ; < = > ? */
  145. P, XU, XU, XU, XU, XU, XU, U, /* @ A B C D E F G */
  146. U, U, U, U, U, U, U, U, /* H I J K L M N O */
  147. U, U, U, U, U, U, U, U, /* P Q R S T U V W */
  148. U, U, U, P, P, P, P, _, /* X Y Z [ \ ] ^ _ */
  149. P, XL, XL, XL, XL, XL, XL, L, /* ` a b c d e f g */
  150. L, L, L, L, L, L, L, L, /* h i j k l m n o */
  151. L, L, L, L, L, L, L, L, /* p q r s t u v w */
  152. L, L, L, P, P, P, P, C, /* x y z { | } ~ DEL */
  153. /* high half of unsigned char is locale-specific, so all tests are
  154. false in "C" locale */
  155. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  156. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  157. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  158. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  159. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  160. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  161. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  162. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  163. };
  164. const unsigned char _sch_tolower[256] =
  165. {
  166. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  167. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  168. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  169. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  170. 64,
  171. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  172. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  173. 91, 92, 93, 94, 95, 96,
  174. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  175. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  176. 123,124,125,126,127,
  177. 128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143,
  178. 144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159,
  179. 160,161,162,163, 164,165,166,167, 168,169,170,171, 172,173,174,175,
  180. 176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,190,191,
  181. 192,193,194,195, 196,197,198,199, 200,201,202,203, 204,205,206,207,
  182. 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223,
  183. 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239,
  184. 240,241,242,243, 244,245,246,247, 248,249,250,251, 252,253,254,255,
  185. };
  186. const unsigned char _sch_toupper[256] =
  187. {
  188. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  189. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  190. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  191. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  192. 64,
  193. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  194. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  195. 91, 92, 93, 94, 95, 96,
  196. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  197. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  198. 123,124,125,126,127,
  199. 128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143,
  200. 144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159,
  201. 160,161,162,163, 164,165,166,167, 168,169,170,171, 172,173,174,175,
  202. 176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,190,191,
  203. 192,193,194,195, 196,197,198,199, 200,201,202,203, 204,205,206,207,
  204. 208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223,
  205. 224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239,
  206. 240,241,242,243, 244,245,246,247, 248,249,250,251, 252,253,254,255,
  207. };
  208. #else
  209. # if HOST_CHARSET == HOST_CHARSET_EBCDIC
  210. #error "FIXME: write tables for EBCDIC"
  211. # else
  212. #error "Unrecognized host character set"
  213. # endif
  214. #endif