* safe-ctype.c: New file.
[platform/upstream/binutils.git] / libiberty / safe-ctype.c
1 /* <ctype.h> replacement macros.
2
3    Copyright (C) 2000 Free Software Foundation, Inc.
4    Contributed by Zack Weinberg <zackw@stanford.edu>.
5
6 This file is part of the libiberty library.
7 Libiberty is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 Libiberty is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public
18 License along with libiberty; see the file COPYING.LIB.  If
19 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* This is a compatible replacement of the standard C library's <ctype.h>
23    with the following properties:
24
25    - Implements all isxxx() macros required by C99.
26    - Also implements some character classes useful when
27      parsing C-like languages.
28    - Does not change behavior depending on the current locale.
29    - Behaves properly for all values in the range of a signed or
30      unsigned char.  */
31
32 #include <safe-ctype.h>
33 #include <stdio.h>  /* for EOF */
34
35 /* Shorthand */
36 #define bl _sch_isblank
37 #define cn _sch_iscntrl
38 #define di _sch_isdigit
39 #define is _sch_isidst
40 #define lo _sch_islower
41 #define nv _sch_isnvsp
42 #define pn _sch_ispunct
43 #define pr _sch_isprint
44 #define sp _sch_isspace
45 #define up _sch_isupper
46 #define vs _sch_isvsp
47 #define xd _sch_isxdigit
48
49 /* Masks.  */
50 #define L  lo|is   |pr  /* lower case letter */
51 #define XL lo|is|xd|pr  /* lowercase hex digit */
52 #define U  up|is   |pr  /* upper case letter */
53 #define XU up|is|xd|pr  /* uppercase hex digit */
54 #define D  di   |xd|pr  /* decimal digit */
55 #define P  pn      |pr  /* punctuation */
56 #define _  pn|is   |pr  /* underscore */
57
58 #define C           cn  /* control character */
59 #define Z  nv      |cn  /* NUL */
60 #define M  nv|sp   |cn  /* cursor movement: \f \v */
61 #define V  vs|sp   |cn  /* vertical space: \r \n */
62 #define T  nv|sp|bl|cn  /* tab */
63 #define S  nv|sp|bl|pr  /* space */
64
65 /* Are we ASCII? */
66 #if '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \
67   && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21 \
68   && EOF == -1
69
70 const unsigned short _sch_istable[256] =
71 {
72   Z,  C,  C,  C,   C,  C,  C,  C,   /* NUL SOH STX ETX  EOT ENQ ACK BEL */
73   C,  T,  V,  M,   M,  V,  C,  C,   /* BS  HT  LF  VT   FF  CR  SO  SI  */
74   C,  C,  C,  C,   C,  C,  C,  C,   /* DLE DC1 DC2 DC3  DC4 NAK SYN ETB */
75   C,  C,  C,  C,   C,  C,  C,  C,   /* CAN EM  SUB ESC  FS  GS  RS  US  */
76   S,  P,  P,  P,   P,  P,  P,  P,   /* SP  !   "   #    $   %   &   '   */
77   P,  P,  P,  P,   P,  P,  P,  P,   /* (   )   *   +    ,   -   .   /   */
78   D,  D,  D,  D,   D,  D,  D,  D,   /* 0   1   2   3    4   5   6   7   */
79   D,  D,  P,  P,   P,  P,  P,  P,   /* 8   9   :   ;    <   =   >   ?   */
80   P, XU, XU, XU,  XU, XU, XU,  U,   /* @   A   B   C    D   E   F   G   */
81   U,  U,  U,  U,   U,  U,  U,  U,   /* H   I   J   K    L   M   N   O   */
82   U,  U,  U,  U,   U,  U,  U,  U,   /* P   Q   R   S    T   U   V   W   */
83   U,  U,  U,  P,   P,  P,  P,  _,   /* X   Y   Z   [    \   ]   ^   _   */
84   P, XL, XL, XL,  XL, XL, XL,  L,   /* `   a   b   c    d   e   f   g   */
85   L,  L,  L,  L,   L,  L,  L,  L,   /* h   i   j   k    l   m   n   o   */
86   L,  L,  L,  L,   L,  L,  L,  L,   /* p   q   r   s    t   u   v   w   */
87   L,  L,  L,  P,   P,  P,  P,  C,   /* x   y   z   {    |   }   ~   DEL */
88
89   /* high half of unsigned char is locale-specific, so all tests are
90      false in "C" locale */
91   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
92   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
93   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
94   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
95
96   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
97   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
98   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
99   0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,
100 };
101
102 const unsigned char _sch_tolower[256] =
103 {
104    0,  1,  2,  3,   4,  5,  6,  7,   8,  9, 10, 11,  12, 13, 14, 15,
105   16, 17, 18, 19,  20, 21, 22, 23,  24, 25, 26, 27,  28, 29, 30, 31,
106   32, 33, 34, 35,  36, 37, 38, 39,  40, 41, 42, 43,  44, 45, 46, 47,
107   48, 49, 50, 51,  52, 53, 54, 55,  56, 57, 58, 59,  60, 61, 62, 63,
108   64,
109
110   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
111   'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
112
113   91, 92, 93, 94, 95, 96,
114
115   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
116   'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
117
118  123,124,125,126,127,
119
120  128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143,
121  144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159,
122  160,161,162,163, 164,165,166,167, 168,169,170,171, 172,173,174,175,
123  176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,190,191,
124
125  192,193,194,195, 196,197,198,199, 200,201,202,203, 204,205,206,207,
126  208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223,
127  224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239,
128  240,241,242,243, 244,245,246,247, 248,249,250,251, 252,253,254,255,
129 };
130
131 const unsigned char _sch_toupper[256] =
132 {
133    0,  1,  2,  3,   4,  5,  6,  7,   8,  9, 10, 11,  12, 13, 14, 15,
134   16, 17, 18, 19,  20, 21, 22, 23,  24, 25, 26, 27,  28, 29, 30, 31,
135   32, 33, 34, 35,  36, 37, 38, 39,  40, 41, 42, 43,  44, 45, 46, 47,
136   48, 49, 50, 51,  52, 53, 54, 55,  56, 57, 58, 59,  60, 61, 62, 63,
137   64,
138
139   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
140   'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
141
142   91, 92, 93, 94, 95, 96,
143
144   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
145   'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
146
147  123,124,125,126,127,
148
149  128,129,130,131, 132,133,134,135, 136,137,138,139, 140,141,142,143,
150  144,145,146,147, 148,149,150,151, 152,153,154,155, 156,157,158,159,
151  160,161,162,163, 164,165,166,167, 168,169,170,171, 172,173,174,175,
152  176,177,178,179, 180,181,182,183, 184,185,186,187, 188,189,190,191,
153
154  192,193,194,195, 196,197,198,199, 200,201,202,203, 204,205,206,207,
155  208,209,210,211, 212,213,214,215, 216,217,218,219, 220,221,222,223,
156  224,225,226,227, 228,229,230,231, 232,233,234,235, 236,237,238,239,
157  240,241,242,243, 244,245,246,247, 248,249,250,251, 252,253,254,255,
158 };
159
160 #else
161  #error "Unsupported host character set"
162 #endif /* not ASCII */