Rizin
unix-like reverse engineering framework and cli tools
safe-ctype.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2000, 2001 Free Software Foundation, Inc.
2 // SPDX-License-Identifier: LGPL-2.0-or-later
3 
4 /* <ctype.h> replacement macros.
5 
6  Copyright (C) 2000, 2001 Free Software Foundation, Inc.
7  Contributed by Zack Weinberg <zackw@stanford.edu>.
8 
9 This file is part of the libiberty library.
10 Libiberty is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
14 
15 Libiberty is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
19 
20 You should have received a copy of the GNU Library General Public
21 License along with libiberty; see the file COPYING.LIB. If
22 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
23 Boston, MA 02110-1301, USA. */
24 
25 /* This is a compatible replacement of the standard C library's <ctype.h>
26  with the following properties:
27 
28  - Implements all isxxx() macros required by C99.
29  - Also implements some character classes useful when
30  parsing C-like languages.
31  - Does not change behavior depending on the current locale.
32  - Behaves properly for all values in the range of a signed or
33  unsigned char.
34 
35  To avoid conflicts, this header defines the isxxx functions in upper
36  case, e.g. ISALPHA not isalpha. */
37 
38 #ifndef SAFE_CTYPE_H
39 #define SAFE_CTYPE_H
40 
41 /* Determine host character set. */
42 #define HOST_CHARSET_UNKNOWN 0
43 #define HOST_CHARSET_ASCII 1
44 #define HOST_CHARSET_EBCDIC 2
45 
46 #if '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21
47 #define HOST_CHARSET HOST_CHARSET_ASCII
48 #else
49 #if '\n' == 0x15 && ' ' == 0x40 && '0' == 0xF0 && 'A' == 0xC1 && 'a' == 0x81 && '!' == 0x5A
50 #define HOST_CHARSET HOST_CHARSET_EBCDIC
51 #else
52 #define HOST_CHARSET HOST_CHARSET_UNKNOWN
53 #endif
54 #endif
55 
56 /* Categories. */
57 
58 enum {
59  /* In C99 */
60  _sch_isblank = 0x0001, /* space \t */
61  _sch_iscntrl = 0x0002, /* nonprinting characters */
62  _sch_isdigit = 0x0004, /* 0-9 */
63  _sch_islower = 0x0008, /* a-z */
64  _sch_isprint = 0x0010, /* any printing character including ' ' */
65  _sch_ispunct = 0x0020, /* all punctuation */
66  _sch_isspace = 0x0040, /* space \t \n \r \f \v */
67  _sch_isupper = 0x0080, /* A-Z */
68  _sch_isxdigit = 0x0100, /* 0-9A-Fa-f */
69 
70  /* Extra categories useful to cpplib. */
71  _sch_isidst = 0x0200, /* A-Za-z_ */
72  _sch_isvsp = 0x0400, /* \n \r */
73  _sch_isnvsp = 0x0800, /* space \t \f \v \0 */
74 
75  /* Combinations of the above. */
77  _sch_isalnum = _sch_isalpha | _sch_isdigit, /* A-Za-z0-9 */
78  _sch_isidnum = _sch_isidst | _sch_isdigit, /* A-Za-z0-9_ */
79  _sch_isgraph = _sch_isalnum | _sch_ispunct, /* isprint and not space */
80  _sch_iscppsp = _sch_isvsp | _sch_isnvsp, /* isspace + \0 */
81  _sch_isbasic = _sch_isprint | _sch_iscppsp /* basic charset of ISO C
82  (plus ` and @) */
83 };
84 
85 /* Character classification. */
86 extern const unsigned short _sch_istable[256];
87 
88 #define _sch_test(c, bit) (_sch_istable[(c)&0xff] & (unsigned short)(bit))
89 
90 #define ISALPHA(c) _sch_test(c, _sch_isalpha)
91 #define ISALNUM(c) _sch_test(c, _sch_isalnum)
92 #define ISBLANK(c) _sch_test(c, _sch_isblank)
93 #define ISCNTRL(c) _sch_test(c, _sch_iscntrl)
94 #define ISDIGIT(c) _sch_test(c, _sch_isdigit)
95 #define ISGRAPH(c) _sch_test(c, _sch_isgraph)
96 #define ISLOWER(c) _sch_test(c, _sch_islower)
97 #define ISPRINT(c) _sch_test(c, _sch_isprint)
98 #define ISPUNCT(c) _sch_test(c, _sch_ispunct)
99 #define ISSPACE(c) _sch_test(c, _sch_isspace)
100 #define ISUPPER(c) _sch_test(c, _sch_isupper)
101 #define ISXDIGIT(c) _sch_test(c, _sch_isxdigit)
102 
103 #define ISIDNUM(c) _sch_test(c, _sch_isidnum)
104 #define ISIDST(c) _sch_test(c, _sch_isidst)
105 #define IS_ISOBASIC(c) _sch_test(c, _sch_isbasic)
106 #define IS_VSPACE(c) _sch_test(c, _sch_isvsp)
107 #define IS_NVSPACE(c) _sch_test(c, _sch_isnvsp)
108 #define IS_SPACE_OR_NUL(c) _sch_test(c, _sch_iscppsp)
109 
110 /* Character transformation. */
111 extern const unsigned char _sch_toupper[256];
112 extern const unsigned char _sch_tolower[256];
113 #define TOUPPER(c) _sch_toupper[(c)&0xff]
114 #define TOLOWER(c) _sch_tolower[(c)&0xff]
115 
116 /* Prevent the users of safe-ctype.h from accidently using the routines
117  from ctype.h. Initially, the approach was to produce an error when
118  detecting that ctype.h has been included. But this was causing
119  trouble as ctype.h might get indirectly included as a result of
120  including another system header (for instance gnulib's stdint.h).
121  So we include ctype.h here and then immediately redefine its macros. */
122 
123 #include <ctype.h>
124 #undef isalpha
125 #define isalpha(c) do_not_use_isalpha_with_safe_ctype
126 #undef isalnum
127 #define isalnum(c) do_not_use_isalnum_with_safe_ctype
128 #undef iscntrl
129 #define iscntrl(c) do_not_use_iscntrl_with_safe_ctype
130 #undef isdigit
131 #define isdigit(c) do_not_use_isdigit_with_safe_ctype
132 #undef isgraph
133 #define isgraph(c) do_not_use_isgraph_with_safe_ctype
134 #undef islower
135 #define islower(c) do_not_use_islower_with_safe_ctype
136 #undef isprint
137 #define isprint(c) do_not_use_isprint_with_safe_ctype
138 #undef ispunct
139 #define ispunct(c) do_not_use_ispunct_with_safe_ctype
140 #undef isspace
141 #define isspace(c) do_not_use_isspace_with_safe_ctype
142 #undef isupper
143 #define isupper(c) do_not_use_isupper_with_safe_ctype
144 #undef isxdigit
145 #define isxdigit(c) do_not_use_isxdigit_with_safe_ctype
146 #undef toupper
147 #define toupper(c) do_not_use_toupper_with_safe_ctype
148 #undef tolower
149 #define tolower(c) do_not_use_tolower_with_safe_ctype
150 
151 #endif /* SAFE_CTYPE_H */
@ _sch_isalpha
Definition: safe-ctype.h:76
@ _sch_isupper
Definition: safe-ctype.h:67
@ _sch_isvsp
Definition: safe-ctype.h:72
@ _sch_isprint
Definition: safe-ctype.h:64
@ _sch_isdigit
Definition: safe-ctype.h:62
@ _sch_islower
Definition: safe-ctype.h:63
@ _sch_isidnum
Definition: safe-ctype.h:78
@ _sch_iscppsp
Definition: safe-ctype.h:80
@ _sch_iscntrl
Definition: safe-ctype.h:61
@ _sch_isxdigit
Definition: safe-ctype.h:68
@ _sch_isblank
Definition: safe-ctype.h:60
@ _sch_isgraph
Definition: safe-ctype.h:79
@ _sch_isspace
Definition: safe-ctype.h:66
@ _sch_isalnum
Definition: safe-ctype.h:77
@ _sch_ispunct
Definition: safe-ctype.h:65
@ _sch_isnvsp
Definition: safe-ctype.h:73
@ _sch_isidst
Definition: safe-ctype.h:71
@ _sch_isbasic
Definition: safe-ctype.h:81
const unsigned char _sch_toupper[256]
const unsigned short _sch_istable[256]
const unsigned char _sch_tolower[256]