safe-ctype.c 10 KB

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