Debian Codegraph @

texk/kpathsea/c-ctype.h

raw
1 /* c-ctype.h: ASCII-safe versions of the <ctype.h> macros.
2
3 Copyright 1992, 1994, 2008, 2010, 2011, 2015, 2025 Karl Berry.
4 Copyright 1998, 2000, 2005 Olaf Weber.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, see <http://www.gnu.org/licenses/>. */
18
19 #ifndef KPATHSEA_C_CTYPE_H
20 #define KPATHSEA_C_CTYPE_H
21
22 #include <ctype.h>
23
24 /* Be sure we have `isascii'. */
25 #ifndef WIN32
26 #if !(defined(HAVE_DECL_ISASCII) && HAVE_DECL_ISASCII)
27 #define isascii(c) (((c) & ~0x7f) == 0)
28 #endif
29 #endif
30
31 /* However, we don't use isascii, since POSIX + compilers have made it
32 impossible to do so portably (first failure reports for gcc 15;
33 underlying reason is unclear). Leave the definition above just
34 because we've always defined it, but for our use here, apparently we
35 now have to define and use our own macro. Discussion:
36 https://tug.org/pipermail/tlbuild/2025q2/005743.html */
37
38 /* We want to possibly accept integers as an argument, because of the
39 old problems described in the comment below. So cast to unsigned
40 since otherwise checking for (i)>0 runs the risk of more useless
41 compiler warnings/errors about redundant comparisons. */
42 #define kpse_isascii(i) ((unsigned) (i) <= 127)
43
44 #define ISALNUM(c) (kpse_isascii (c) && isalnum((unsigned char)c))
45 #define ISALPHA(c) (kpse_isascii (c) && isalpha((unsigned char)c))
46 #define KPSE_ISASCII kpse_isascii
47 #define ISCNTRL(c) (kpse_isascii (c) && iscntrl((unsigned char)c))
48 #define ISDIGIT(c) (kpse_isascii (c) && isdigit ((unsigned char)c))
49 #define ISGRAPH(c) (kpse_isascii (c) && isgraph((unsigned char)c))
50 #define ISLOWER(c) (kpse_isascii (c) && islower((unsigned char)c))
51 #define ISPRINT(c) (kpse_isascii (c) && isprint((unsigned char)c))
52 #define ISPUNCT(c) (kpse_isascii (c) && ispunct((unsigned char)c))
53 #define ISSPACE(c) (kpse_isascii (c) && isspace((unsigned char)c))
54 #define ISUPPER(c) (kpse_isascii (c) && isupper((unsigned char)c))
55 #define ISXDIGIT(c) (kpse_isascii (c) && isxdigit((unsigned char)c))
56 #define TOASCII toascii
57 #define TOLOWER(c) (ISUPPER (c) ? tolower ((unsigned char)c) : (c))
58 #define TOUPPER(c) (ISLOWER (c) ? toupper ((unsigned char)c) : (c))
59
60 /* This isn't part of the usual <ctype.h>, but it's useful sometimes.
61 We don't consider vertical tab blank, since in normal use, it isn't. */
62 #ifndef isblank
63 #define isblank(c) ((c) == ' ' || (c) == '\t')
64 #endif
65
66 #define ISBLANK(c) (kpse_isascii (c) && isblank ((unsigned char)c))
67
68
69 /* Here's why this mess is necessary:
70
71 From: [email protected] (Jim Meyering)
72 Date: Wed, 25 Nov 1992 09:52:33 -0600
73 Subject: ss-921123: using isascii with <ctype.h> macros
74
75 Yesterday some cursory regression testing found that GNU od
76 (in an upcoming release of textutils) generated incorrect output
77 when run on an SGI indigo because isprint ('\377') returned true.
78 Of course, '\377' is not a printing character; the problem lay
79 in using isprint without first making sure its integer argument
80 corresponded to an ascii code.
81
82 MORAL: always guard uses of ctype macros with isascii if it's available.
83 An obvious alternative is to avoid <ctype.h> and define and use your
84 own versions of the ctype macros.
85
86 A pretty clean approach to using <ctype.h> and isascii was
87 suggested by David MacKenzie: [but as of 2025, no longer works; see above]
88
89 #ifndef isascii
90 #define isascii(c) 1
91 #endif
92
93 #define ISDIGIT(c) (isascii (c) && isdigit (c))
94 #define ISPRINT(c) (isascii (c) && isprint (c))
95 ...
96
97 then, use ISDIGIT, etc. instead of isdigit, etc. */
98
99 #endif /* not KPATHSEA_C_CTYPE_H */