[PATCH v2 1/2] Add CTYPE defines for various sets of characters.
"Kevin J. McCarthy" <[email protected]> Tue, 4 Aug 2026 13:18:19 +0800
| Newsgroups | gmane.mail.mutt.devel |
|---|---|
| Message-ID | <[email protected]> |
Thanks to Alejandro Colomar for this suggestion, and I used his
examples directly for LOWER_C to PFCHAR_C. The HEX additions are
mine, so you can blame those on me. ;-)
---
Please let me know what you think. Does it help readability for the
cases where we use the CTYPE along with extra characters?
e.g. imap/command.c, lib.c, muttlib.c below.
crypt.c | 2 +-
imap/command.c | 2 +-
lib.c | 2 +-
lib.h | 13 +++++++++++++
mutt_sasl_gnu.c | 2 +-
muttlib.c | 2 +-
rfc2047.c | 2 +-
url.c | 2 +-
8 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/crypt.c b/crypt.c
index e4c8051d..d141ac6f 100644
--- a/crypt.c
+++ b/crypt.c
@@ -1295,7 +1295,7 @@ short crypt_is_numerical_keyid(const char *s)
if (strlen(s) % 8)
return 0;
while (*s)
- if (strchr("0123456789ABCDEFabcdef", *s++) == NULL)
+ if (strchr(CTYPE_HEX_C, *s++) == NULL)
return 0;
return 1;
diff --git a/imap/command.c b/imap/command.c
index 63dedf6c..94c98883 100644
--- a/imap/command.c
+++ b/imap/command.c
@@ -701,7 +701,7 @@ static void cmd_parse_vanished(IMAP_DATA *idata, char *s)
end_of_seqset = s;
while (*end_of_seqset)
{
- if (!strchr("0123456789:,", *end_of_seqset))
+ if (!strchr(CTYPE_DIGIT_C ":,", *end_of_seqset))
*end_of_seqset = '\0';
else
end_of_seqset++;
diff --git a/lib.c b/lib.c
index 9776fcdb..fdecda25 100644
--- a/lib.c
+++ b/lib.c
@@ -573,7 +573,7 @@ success:
}
-static const char safe_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+@{}._-:%";
+static const char safe_chars[] = CTYPE_PFCHAR_C "+@{}:%";
void mutt_sanitize_filename(char *f, int flags)
{
diff --git a/lib.h b/lib.h
index 25875656..b622e9c0 100644
--- a/lib.h
+++ b/lib.h
@@ -147,6 +147,19 @@ static inline char *skip_email_wsp(const char *s)
on some systems */
#define SKIP_LOCALE_WS(c) while (*(c) && IS_LOCALE_WS(*(c))) c++;
+/*
+ * Various useful sets of characters
+ */
+#define CTYPE_LOWER_C "abcdefghijklmnopqrstuvwxyz"
+#define CTYPE_UPPER_C "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+#define CTYPE_DIGIT_C "0123456789"
+#define CTYPE_ALPHA_C CTYPE_LOWER_C CTYPE_UPPER_C
+#define CTYPE_ALNUM_C CTYPE_ALPHA_C CTYPE_DIGIT_C
+#define CTYPE_PFCHAR_C CTYPE_ALNUM_C "._-" // POSIX.1-2008 portable
+ // filename character set
+#define CTYPE_UHEX_C CTYPE_DIGIT_C "ABCDEF" // uppercase hex
+#define CTYPE_HEX_C CTYPE_UHEX_C "abcdef"
+
/*
* These functions aren't defined in lib.c, but
* they are used there.
diff --git a/mutt_sasl_gnu.c b/mutt_sasl_gnu.c
index 9a7a4b87..2bd5afbf 100644
--- a/mutt_sasl_gnu.c
+++ b/mutt_sasl_gnu.c
@@ -66,7 +66,7 @@ void mutt_gsasl_done(void)
}
static const char *VALID_MECHANISM_CHARACTERS =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
+ CTYPE_UPPER_C CTYPE_DIGIT_C "-_";
/* This logic is derived from the libgsasl suggest code */
static int mechlist_contains(const char *uc_mech, const char *uc_mechlist)
diff --git a/muttlib.c b/muttlib.c
index d24a37f0..ee29cc17 100644
--- a/muttlib.c
+++ b/muttlib.c
@@ -1178,7 +1178,7 @@ void _mutt_buffer_quote_filename(BUFFER *d, const char *f, int add_outer)
mutt_buffer_addch(d, '\'');
}
-static const char safe_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+@{}._-:%";
+static const char safe_chars[] = CTYPE_PFCHAR_C "+@{}:%";
void mutt_buffer_sanitize_filename(BUFFER *d, const char *f, int flags)
{
diff --git a/rfc2047.c b/rfc2047.c
index 2bdd8553..1bacb54f 100644
--- a/rfc2047.c
+++ b/rfc2047.c
@@ -257,7 +257,7 @@ static size_t b_encoder(char *s, ICONV_CONST char *d, size_t dlen,
static size_t q_encoder(char *s, ICONV_CONST char *d, size_t dlen,
const char *tocode)
{
- static const char hex[] = "0123456789ABCDEF";
+ static const char hex[] = CTYPE_UHEX_C;
char *s0 = s;
memcpy(s, "=?", 2), s += 2;
diff --git a/url.c b/url.c
index 02dbf34c..176877cd 100644
--- a/url.c
+++ b/url.c
@@ -193,7 +193,7 @@ int url_parse_ciss(ciss_url_t *ciss, char *src)
static void url_pct_encode(char *dst, size_t l, const char *src)
{
- static const char *alph = "0123456789ABCDEF";
+ static const char *alph = CTYPE_UHEX_C;
*dst = 0;
l--;
--
2.55.0