From patchwork Sun Feb 26 11:50:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13152371 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5486CC7EE2D for ; Sun, 26 Feb 2023 11:50:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229540AbjBZLu6 (ORCPT ); Sun, 26 Feb 2023 06:50:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbjBZLuz (ORCPT ); Sun, 26 Feb 2023 06:50:55 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A97F1E069 for ; Sun, 26 Feb 2023 03:50:54 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 16AD560BA0 for ; Sun, 26 Feb 2023 11:50:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ADA49C433D2; Sun, 26 Feb 2023 11:50:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677412253; bh=YQbc0GVM9Q2ByFmQDmGltDBQskLZLau2TXPMg/dQM/4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kjPAVc2sspFXuowK0/L2PN/itlMkmXuKE4/EK2asX7+H6C291UymnJBVFbCNu4965 JosB8/OWGee8dpp6vbD2p1Z5z2po92YpxDYDLawh/JmStc8bnFuRvbG7nmX1423Z+d T3BQD5+5hNpV6QpccfOTr84V+6PC0tRBu12m1cOhPbPvwcyNm4wPjhaVjv/rmzAc/+ 9iW8pbp/pxr4MQBNlDUQME4+5MUeVtHNYqfY/gD53Zwa8emo7T0CMVpnjzJn6pFlp/ RTKF3+apOTKuEQaqu4jCN0jiYvHFWUyg8kBrlskmlMuBlfukZU1Ev/r9Tep5jZnPfi xUf57amfWW5Dw== From: Masahiro Yamada To: git@vger.kernel.org Cc: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , Masahiro Yamada Subject: [PATCH v2 1/5] git-compat-util: add isblank() and isgraph() Date: Sun, 26 Feb 2023 20:50:17 +0900 Message-Id: <20230226115021.1681834-2-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230226115021.1681834-1-masahiroy@kernel.org> References: <20230226115021.1681834-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org git-compat-util.h implements most of is*() macros. Add isblank() and isgraph(), which are useful to clean up wildmatch.c in a consistent way (in this and later commits). In the previous submission, I just moved isblank() and isgraph() as implemented in wildmatch.c. I knew they were not robust against the pointer increment like isblank(*s++), but I thought it was the same pattern as isprint(), which has the same issue. Unfortunately, it was more controversial than I had expected... This version implements them as inline functions because we ran out all bits in the sane_ctype[] table. This is the same pattern as islower() and isupper(). Once we refactor ctype.c to create more room in sane_ctype[], isblank() and isgraph() will be able to use sane_istest(). Probably so will islower() and isupper(). The ctype in Linux kernel (lib/ctype.c) has the LOWER and UPPER bits separately. Signed-off-by: Masahiro Yamada --- Changes in v2: - Use inline functions git-compat-util.h | 14 ++++++++++++++ wildmatch.c | 14 ++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/git-compat-util.h b/git-compat-util.h index 4f0028ce60..b29c238f02 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1212,10 +1212,12 @@ extern const unsigned char tolower_trans_tbl[256]; /* Sane ctype - no locale, and works with signed chars */ #undef isascii #undef isspace +#undef isblank #undef isdigit #undef isalpha #undef isalnum #undef isprint +#undef isgraph #undef islower #undef isupper #undef tolower @@ -1236,10 +1238,12 @@ extern const unsigned char sane_ctype[256]; #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0) #define isascii(x) (((x) & ~0x7f) == 0) #define isspace(x) sane_istest(x,GIT_SPACE) +#define isblank(x) sane_isblank(x) #define isdigit(x) sane_istest(x,GIT_DIGIT) #define isalpha(x) sane_istest(x,GIT_ALPHA) #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT) #define isprint(x) ((x) >= 0x20 && (x) <= 0x7e) +#define isgraph(x) sane_isgraph(x) #define islower(x) sane_iscase(x, 1) #define isupper(x) sane_iscase(x, 0) #define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL) @@ -1270,6 +1274,16 @@ static inline int sane_iscase(int x, int is_lower) return (x & 0x20) == 0; } +static inline int sane_isblank(int c) +{ + return c == ' ' || c == '\t'; +} + +static inline int sane_isgraph(int c) +{ + return isprint(c) && !isspace(c); +} + /* * Like skip_prefix, but compare case-insensitively. Note that the comparison * is done via tolower(), so it is strictly ASCII (no multi-byte characters or diff --git a/wildmatch.c b/wildmatch.c index 7e5a7ea1ea..85c4c7f8a7 100644 --- a/wildmatch.c +++ b/wildmatch.c @@ -28,18 +28,8 @@ typedef unsigned char uchar; # define ISASCII(c) isascii(c) #endif -#ifdef isblank -# define ISBLANK(c) (ISASCII(c) && isblank(c)) -#else -# define ISBLANK(c) ((c) == ' ' || (c) == '\t') -#endif - -#ifdef isgraph -# define ISGRAPH(c) (ISASCII(c) && isgraph(c)) -#else -# define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c)) -#endif - +#define ISBLANK(c) (ISASCII(c) && isblank(c)) +#define ISGRAPH(c) (ISASCII(c) && isgraph(c)) #define ISPRINT(c) (ISASCII(c) && isprint(c)) #define ISDIGIT(c) (ISASCII(c) && isdigit(c)) #define ISALNUM(c) (ISASCII(c) && isalnum(c)) From patchwork Sun Feb 26 11:50:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13152372 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC393C7EE2F for ; Sun, 26 Feb 2023 11:51:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229547AbjBZLu6 (ORCPT ); Sun, 26 Feb 2023 06:50:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229512AbjBZLu4 (ORCPT ); Sun, 26 Feb 2023 06:50:56 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B09A9E06D for ; Sun, 26 Feb 2023 03:50:55 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4231160BDC for ; Sun, 26 Feb 2023 11:50:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E05E7C433EF; Sun, 26 Feb 2023 11:50:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677412254; bh=Sj/UmPBpg661lyCVCVadLaJE1snK6QiP1eCNv3svn7A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hSqrMkIsFMr+gFTcncUaf40QHQEk79vLj7f1UnX9fixva/8ZOB4m0ty3szhrcO8Kg tQyFjXSQVVyYTkXZfUplB7Wt9dud5rZ8hBK+tGaNYa25er0UMRLZ68VkC5rRxGu7dB 3Ee6nRK8tCCQHbSq/h6DrOyjhga8Jxj++mdEy+u2pnwIrd80DYUZ0GRPn3j85uuWTR lDrnT+Hxp7KptFsmY0xLHaKy8SIL8AqgH7va5a8Sb+oudIIkZKHR4XqDwAEtVZGk+l CaXETv8lfeBpumLy17qwgHX0uLGzQqR7X9zmbsfxj2m1xZC+7pDWGF+TV1mvWZX5fp czMdYhMq7CPsA== From: Masahiro Yamada To: git@vger.kernel.org Cc: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , Masahiro Yamada Subject: [PATCH v2 2/5] wildmatch: remove IS*() macros Date: Sun, 26 Feb 2023 20:50:18 +0900 Message-Id: <20230226115021.1681834-3-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230226115021.1681834-1-masahiroy@kernel.org> References: <20230226115021.1681834-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org This file was imported from rsync, which has some compatibility layer because it relies on in C standard library. In contrast, GIT has its own implementations in git-compat-util.h. [1] isprint, isgraph, isblank They check the given char range in an obvious way [2] isspace, isdigit, isalpha, isalnum, islower, isupper, iscntr, ispunct They look up sane_ctype[], which fills the range 0x80-0xff with 0. [3] isxdigit It looks up hexval_table[], which fills the range 0x80-0xff with -1. For all of these, ISACII() is a redundant check. Remove IS*() macros, and directly use is*() in dowild(). Signed-off-by: Masahiro Yamada --- (no changes since v1) wildmatch.c | 55 ++++++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/wildmatch.c b/wildmatch.c index 85c4c7f8a7..a510b3fd23 100644 --- a/wildmatch.c +++ b/wildmatch.c @@ -22,25 +22,6 @@ typedef unsigned char uchar; && *(class) == *(litmatch) \ && strncmp((char*)class, litmatch, len) == 0) -#if defined STDC_HEADERS || !defined isascii -# define ISASCII(c) 1 -#else -# define ISASCII(c) isascii(c) -#endif - -#define ISBLANK(c) (ISASCII(c) && isblank(c)) -#define ISGRAPH(c) (ISASCII(c) && isgraph(c)) -#define ISPRINT(c) (ISASCII(c) && isprint(c)) -#define ISDIGIT(c) (ISASCII(c) && isdigit(c)) -#define ISALNUM(c) (ISASCII(c) && isalnum(c)) -#define ISALPHA(c) (ISASCII(c) && isalpha(c)) -#define ISCNTRL(c) (ISASCII(c) && iscntrl(c)) -#define ISLOWER(c) (ISASCII(c) && islower(c)) -#define ISPUNCT(c) (ISASCII(c) && ispunct(c)) -#define ISSPACE(c) (ISASCII(c) && isspace(c)) -#define ISUPPER(c) (ISASCII(c) && isupper(c)) -#define ISXDIGIT(c) (ISASCII(c) && isxdigit(c)) - /* Match pattern "p" against "text" */ static int dowild(const uchar *p, const uchar *text, unsigned int flags) { @@ -52,9 +33,9 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags) uchar t_ch, prev_ch; if ((t_ch = *text) == '\0' && p_ch != '*') return WM_ABORT_ALL; - if ((flags & WM_CASEFOLD) && ISUPPER(t_ch)) + if ((flags & WM_CASEFOLD) && isupper(t_ch)) t_ch = tolower(t_ch); - if ((flags & WM_CASEFOLD) && ISUPPER(p_ch)) + if ((flags & WM_CASEFOLD) && isupper(p_ch)) p_ch = tolower(p_ch); switch (p_ch) { case '\\': @@ -133,11 +114,11 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags) */ if (!is_glob_special(*p)) { p_ch = *p; - if ((flags & WM_CASEFOLD) && ISUPPER(p_ch)) + if ((flags & WM_CASEFOLD) && isupper(p_ch)) p_ch = tolower(p_ch); while ((t_ch = *text) != '\0' && (match_slash || t_ch != '/')) { - if ((flags & WM_CASEFOLD) && ISUPPER(t_ch)) + if ((flags & WM_CASEFOLD) && isupper(t_ch)) t_ch = tolower(t_ch); if (t_ch == p_ch) break; @@ -186,7 +167,7 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags) } if (t_ch <= p_ch && t_ch >= prev_ch) matched = 1; - else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch)) { + else if ((flags & WM_CASEFOLD) && islower(t_ch)) { uchar t_ch_upper = toupper(t_ch); if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch) matched = 1; @@ -208,42 +189,42 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags) continue; } if (CC_EQ(s,i, "alnum")) { - if (ISALNUM(t_ch)) + if (isalnum(t_ch)) matched = 1; } else if (CC_EQ(s,i, "alpha")) { - if (ISALPHA(t_ch)) + if (isalpha(t_ch)) matched = 1; } else if (CC_EQ(s,i, "blank")) { - if (ISBLANK(t_ch)) + if (isblank(t_ch)) matched = 1; } else if (CC_EQ(s,i, "cntrl")) { - if (ISCNTRL(t_ch)) + if (iscntrl(t_ch)) matched = 1; } else if (CC_EQ(s,i, "digit")) { - if (ISDIGIT(t_ch)) + if (isdigit(t_ch)) matched = 1; } else if (CC_EQ(s,i, "graph")) { - if (ISGRAPH(t_ch)) + if (isgraph(t_ch)) matched = 1; } else if (CC_EQ(s,i, "lower")) { - if (ISLOWER(t_ch)) + if (islower(t_ch)) matched = 1; } else if (CC_EQ(s,i, "print")) { - if (ISPRINT(t_ch)) + if (isprint(t_ch)) matched = 1; } else if (CC_EQ(s,i, "punct")) { - if (ISPUNCT(t_ch)) + if (ispunct(t_ch)) matched = 1; } else if (CC_EQ(s,i, "space")) { - if (ISSPACE(t_ch)) + if (isspace(t_ch)) matched = 1; } else if (CC_EQ(s,i, "upper")) { - if (ISUPPER(t_ch)) + if (isupper(t_ch)) matched = 1; - else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch)) + else if ((flags & WM_CASEFOLD) && islower(t_ch)) matched = 1; } else if (CC_EQ(s,i, "xdigit")) { - if (ISXDIGIT(t_ch)) + if (isxdigit(t_ch)) matched = 1; } else /* malformed [:class:] string */ return WM_ABORT_ALL; From patchwork Sun Feb 26 11:50:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13152374 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08418C64ED6 for ; Sun, 26 Feb 2023 11:51:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229566AbjBZLvA (ORCPT ); Sun, 26 Feb 2023 06:51:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229520AbjBZLu5 (ORCPT ); Sun, 26 Feb 2023 06:50:57 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC6CBEC7C for ; Sun, 26 Feb 2023 03:50:56 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5813360BA0 for ; Sun, 26 Feb 2023 11:50:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 17C61C433D2; Sun, 26 Feb 2023 11:50:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677412255; bh=BwixfVR8wMjbHtZ5pHJYVGx3u/kD1DVutdeBe/BZV4s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h3GYrGKILblHryyQZwkqcCTG7jTdebNMgE+ETJxgK4I6eSkPt1BIICnS0+fNPHAJi G5QI8rTgoTVN3MkRq+HAiZl3KKAgIdgL7TFgY2TeydGWfAa/bflR6HdFllz8cEOtyG 4UxbH7wxcrRi/8R9nITKDdCglmNWWxWFi5Pjve2ocjHaqIPUq8Zn0uP8Kl34vV8Ze4 DI4+FbFaMJ99ZedzuP42osVukVIVkVRIeREaV39gEOKsJDeDMl88owxPUNfurVc8gX vfePup2atSlOq4gAU+wuJ+Z680XTvqDfwtmmhP1HFOM4RpMUxuVX8mtE1OPAJe+I9V el54UBiKZrCiQ== From: Masahiro Yamada To: git@vger.kernel.org Cc: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , Masahiro Yamada Subject: [PATCH v2 3/5] wildmatch: remove NEGATE_CLASS(2) macros with trivial refactoring Date: Sun, 26 Feb 2023 20:50:19 +0900 Message-Id: <20230226115021.1681834-4-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230226115021.1681834-1-masahiroy@kernel.org> References: <20230226115021.1681834-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The other glob patterns are hard-coded in dowild(). There is no need to macrofy '!' or '^'. Remove the NEGATE_CLASS and REGATE_CLASS2 defines, then refactor the code. Signed-off-by: Masahiro Yamada --- (no changes since v1) wildmatch.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/wildmatch.c b/wildmatch.c index a510b3fd23..93800b8eac 100644 --- a/wildmatch.c +++ b/wildmatch.c @@ -14,10 +14,6 @@ typedef unsigned char uchar; -/* What character marks an inverted character class? */ -#define NEGATE_CLASS '!' -#define NEGATE_CLASS2 '^' - #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \ && *(class) == *(litmatch) \ && strncmp((char*)class, litmatch, len) == 0) @@ -137,12 +133,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags) return WM_ABORT_ALL; case '[': p_ch = *++p; -#ifdef NEGATE_CLASS2 - if (p_ch == NEGATE_CLASS2) - p_ch = NEGATE_CLASS; -#endif /* Assign literal 1/0 because of "matched" comparison. */ - negated = p_ch == NEGATE_CLASS ? 1 : 0; + negated = p_ch == '!' || p_ch == '^' ? 1 : 0; if (negated) { /* Inverted character class. */ p_ch = *++p; From patchwork Sun Feb 26 11:50:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13152375 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09078C6FA8E for ; Sun, 26 Feb 2023 11:51:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229582AbjBZLvD (ORCPT ); Sun, 26 Feb 2023 06:51:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229554AbjBZLu7 (ORCPT ); Sun, 26 Feb 2023 06:50:59 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC69FE06D for ; Sun, 26 Feb 2023 03:50:57 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 88E3260BDC for ; Sun, 26 Feb 2023 11:50:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2E79CC4339E; Sun, 26 Feb 2023 11:50:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677412257; bh=d2802xxHWrXl/CAyafx2bugdRTBTuXVTqf9FtrOF1/Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eoIfNyU70+HZKdOoDgnN/w0v14YVZmJ6PU/UZClxguv1S6Iq6NCRiVU/MgtgfifZj qB76NFK5XguonCHaAxWlV1agBN2GNzuQmZAj+1yJ5APKHzgskjS5xXzYj8FwL7f6SX FbawH3ltbYkaf3CSz+gPHwySoTgFMurRmgYg8sp4+Fdf6nK6vh0p/roueipyGo1VOk PZGzKAYG6WaQezeFPmAPFKnPp+uKHXskBFYoLJBX/1waH04J29fzV4ll9AM66m/ApB 6Y4wbXlY0cB5QXtRdctLsKKJUXyE63l3Q6/aVjUy3yLVjSSCpYRahto/JjjZGJ7/1G i1UOQWuJEoJWg== From: Masahiro Yamada To: git@vger.kernel.org Cc: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , Masahiro Yamada Subject: [PATCH v2 4/5] wildmatch: use char instead of uchar Date: Sun, 26 Feb 2023 20:50:20 +0900 Message-Id: <20230226115021.1681834-5-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230226115021.1681834-1-masahiroy@kernel.org> References: <20230226115021.1681834-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org dowild() casts (char *) and (uchar *) back-and-forth, which is ugly. This file was imported from rsync, which started to use (unsigned char) since the following commit: | commit e11c42511903adc6d27cf1671cc76fa711ea37e5 | Author: Wayne Davison | Date: Sun Jul 6 04:33:54 2003 +0000 | | - Added [:class:] handling to the character-class code. | - Use explicit unsigned characters for proper set checks. | - Made the character-class code honor backslash escapes. | - Accept '^' as a class-negation character in addition to '!'. Perhaps, it was needed because rsync relies on is*() from . GIT has its own implementations, so the behavior is clear. In fact, commit 4546738b58a0 ("Unlocalized isspace and friends") says one of the motivations is "we want the right signed behaviour". sane_istest() casts the given character to (unsigned char) anyway before sane_ctype[] table lookup, so dowild() can use 'char'. Signed-off-by: Masahiro Yamada --- (no changes since v1) wildmatch.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/wildmatch.c b/wildmatch.c index 93800b8eac..7dffd783cb 100644 --- a/wildmatch.c +++ b/wildmatch.c @@ -12,21 +12,19 @@ #include "cache.h" #include "wildmatch.h" -typedef unsigned char uchar; - #define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \ && *(class) == *(litmatch) \ - && strncmp((char*)class, litmatch, len) == 0) + && strncmp(class, litmatch, len) == 0) /* Match pattern "p" against "text" */ -static int dowild(const uchar *p, const uchar *text, unsigned int flags) +static int dowild(const char *p, const char *text, unsigned int flags) { - uchar p_ch; - const uchar *pattern = p; + char p_ch; + const char *pattern = p; for ( ; (p_ch = *p) != '\0'; text++, p++) { int matched, match_slash, negated; - uchar t_ch, prev_ch; + char t_ch, prev_ch; if ((t_ch = *text) == '\0' && p_ch != '*') return WM_ABORT_ALL; if ((flags & WM_CASEFOLD) && isupper(t_ch)) @@ -50,7 +48,7 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags) continue; case '*': if (*++p == '*') { - const uchar *prev_p = p - 2; + const char *prev_p = p - 2; while (*++p == '*') {} if (!(flags & WM_PATHNAME)) /* without WM_PATHNAME, '*' == '**' */ @@ -90,10 +88,10 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags) * with WM_PATHNAME matches the next * directory */ - const char *slash = strchr((char*)text, '/'); + const char *slash = strchr(text, '/'); if (!slash) return WM_NOMATCH; - text = (const uchar*)slash; + text = slash; /* the slash is consumed by the top-level for loop */ break; } @@ -160,13 +158,13 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags) if (t_ch <= p_ch && t_ch >= prev_ch) matched = 1; else if ((flags & WM_CASEFOLD) && islower(t_ch)) { - uchar t_ch_upper = toupper(t_ch); + char t_ch_upper = toupper(t_ch); if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch) matched = 1; } p_ch = 0; /* This makes "prev_ch" get set to 0. */ } else if (p_ch == '[' && p[1] == ':') { - const uchar *s; + const char *s; int i; for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} /*SHARED ITERATOR*/ if (!p_ch) @@ -237,5 +235,5 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags) /* Match the "pattern" against the "text" string. */ int wildmatch(const char *pattern, const char *text, unsigned int flags) { - return dowild((const uchar*)pattern, (const uchar*)text, flags); + return dowild(pattern, text, flags); } From patchwork Sun Feb 26 11:50:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13152373 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4A2AC6FA8E for ; Sun, 26 Feb 2023 11:51:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229562AbjBZLvC (ORCPT ); Sun, 26 Feb 2023 06:51:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229557AbjBZLvA (ORCPT ); Sun, 26 Feb 2023 06:51:00 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9E92125B8 for ; Sun, 26 Feb 2023 03:50:58 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BFA0160BA0 for ; Sun, 26 Feb 2023 11:50:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67506C433EF; Sun, 26 Feb 2023 11:50:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677412258; bh=V+WpyRZT+JcXeERwuCa4/1lyuqv2sT1yHek9aQozq1A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GnfnRgjzwB8Sm2GzZrm2eaTCW7A1dpwzEXmuyInkg/nqyvFohu/qI/dtnhCocc/Bs h0LUQ7tI8NneDPPbF7gs3BNvrn2rpO/CBMd59W/9CEYj2fmURHzQ1ts6U526NChsHq +fC629/hqVnDEefRjkviq3JdoRWcNfHY4YMiYY0kHdBQL83IiJp6DJm+YU8QeSliIG zJYefbZS1rYFVGiroP+ocpafyrCAL5cQ2/AJvrMFUfilC5b5T2alhofAVWVwgHm8KQ fu6L+n24GrVM7m6JdgCCcnhwjlpGi9sC5vwJmBTjq+WOh8+B1wj2Wqt4vij+wX466Z aMdyW8ut3ItjA== From: Masahiro Yamada To: git@vger.kernel.org Cc: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= , Masahiro Yamada Subject: [PATCH v2 5/5] wildmatch: more cleanups after killing uchar Date: Sun, 26 Feb 2023 20:50:21 +0900 Message-Id: <20230226115021.1681834-6-masahiroy@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230226115021.1681834-1-masahiroy@kernel.org> References: <20230226115021.1681834-1-masahiroy@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Remove the local function dowild(), which is now equivalent to wildmatch(). Remove the local variable, slash. Signed-off-by: Masahiro Yamada --- (no changes since v1) wildmatch.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/wildmatch.c b/wildmatch.c index 7dffd783cb..24577e9b8e 100644 --- a/wildmatch.c +++ b/wildmatch.c @@ -17,7 +17,7 @@ && strncmp(class, litmatch, len) == 0) /* Match pattern "p" against "text" */ -static int dowild(const char *p, const char *text, unsigned int flags) +int wildmatch(const char *p, const char *text, unsigned int flags) { char p_ch; const char *pattern = p; @@ -66,7 +66,7 @@ static int dowild(const char *p, const char *text, unsigned int flags) * both foo/bar and foo/a/bar. */ if (p[0] == '/' && - dowild(p + 1, text, flags) == WM_MATCH) + wildmatch(p + 1, text, flags) == WM_MATCH) return WM_MATCH; match_slash = 1; } else /* WM_PATHNAME is set */ @@ -88,10 +88,9 @@ static int dowild(const char *p, const char *text, unsigned int flags) * with WM_PATHNAME matches the next * directory */ - const char *slash = strchr(text, '/'); - if (!slash) + text = strchr(text, '/'); + if (!text) return WM_NOMATCH; - text = slash; /* the slash is consumed by the top-level for loop */ break; } @@ -121,7 +120,7 @@ static int dowild(const char *p, const char *text, unsigned int flags) if (t_ch != p_ch) return WM_NOMATCH; } - if ((matched = dowild(p, text, flags)) != WM_NOMATCH) { + if ((matched = wildmatch(p, text, flags)) != WM_NOMATCH) { if (!match_slash || matched != WM_ABORT_TO_STARSTAR) return matched; } else if (!match_slash && t_ch == '/') @@ -231,9 +230,3 @@ static int dowild(const char *p, const char *text, unsigned int flags) return *text ? WM_NOMATCH : WM_MATCH; } - -/* Match the "pattern" against the "text" string. */ -int wildmatch(const char *pattern, const char *text, unsigned int flags) -{ - return dowild(pattern, text, flags); -}