threads / patch / 52867

patchquote: use isalnum() to check for alphanumeric characters

Subject: [PATCH] quote: use isalnum() to check for alphanumeric characters

## tl;dr

2 messages between Feb 22, 2020 and Feb 23, 2020. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

René Scharfe· Feb 22, 2020, 18:51 UTC · lore

isalnum(c) is equivalent to isalpha(c) || isdigit(c), so use the former instead. The result is shorter, simpler and slightly more efficient.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 quote.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to quote.c +1 −2
diff --git a/quote.c b/quote.c
index 24a58ba454..bcc0dbc50d 100644
--- a/quote.c
+++ b/quote.c
@@ -55,7 +55,7 @@ void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
 	}

 	for (p = src; *p; p++) {
-		if (!isalpha(*p) && !isdigit(*p) && !strchr(ok_punct, *p)) {
+		if (!isalnum(*p) && !strchr(ok_punct, *p)) {
 			sq_quote_buf(dst, src);
 			return;
 		}
--
2.25.1
Martin Ågren· Feb 23, 2020, 09:13 UTC · re: René Scharfe · lore

Re: [PATCH] quote: use isalnum() to check for alphanumeric characters

On Sat, 22 Feb 2020 at 19:53, René Scharfe <l.s.r@web.de> wrote:
>
> isalnum(c) is equivalent to isalpha(c) || isdigit(c), so use the
> former instead.  The result is shorter, simpler and slightly more
> efficient.

From git-compat-util.h I can see that the claim about equivalence is correct. And the efficiency claim, too. And agreed on "shorter and simpler".

Show 8 quoted lines
> --- a/quote.c
> +++ b/quote.c
> @@ -55,7 +55,7 @@ void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
>         }
>
>         for (p = src; *p; p++) {
> -               if (!isalpha(*p) && !isdigit(*p) && !strchr(ok_punct, *p)) {
> +               if (!isalnum(*p) && !strchr(ok_punct, *p)) {
I failed to identify any similar constructs. Looks good to me.
Martin

← back to recent threads