Skip to contentSkip to conversation

wildmatch: properly fold case everywhere

PATCH In review Anthony Ramine 4 participants 18 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wildmatch: properly fold case everywhere

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case fast.

That optimization is kept and ISLOWER() is used in the [:upper:] case when case folding is requested, while matching against a character range is retried with toupper() if the character was lowercase.

---
 t/t3070-wildmatch.sh | 43 +++++++++++++++++++++++++++++++++++++------
 wildmatch.c          |  7 +++++++
 2 files changed, 44 insertions(+), 6 deletions(-)
t/t3070-wildmatch.sh +37 -6
index 4c37057..17315aa 100755
@@ -6,20 +6,20 @@ test_description='wildmatch tests'
 
 match() {
     if [ $1 = 1 ]; then
-	test_expect_success "wildmatch:    match '$3' '$4'" "
+	test_expect_success "wildmatch:     match '$3' '$4'" "
 	    test-wildmatch wildmatch '$3' '$4'
 	"
     else
-	test_expect_success "wildmatch: no match '$3' '$4'" "
+	test_expect_success "wildmatch:  no match '$3' '$4'" "
 	    ! test-wildmatch wildmatch '$3' '$4'
 	"
     fi
     if [ $2 = 1 ]; then
-	test_expect_success "fnmatch:      match '$3' '$4'" "
+	test_expect_success "fnmatch:       match '$3' '$4'" "
 	    test-wildmatch fnmatch '$3' '$4'
 	"
     elif [ $2 = 0 ]; then
-	test_expect_success "fnmatch:   no match '$3' '$4'" "
+	test_expect_success "fnmatch:    no match '$3' '$4'" "
 	    ! test-wildmatch fnmatch '$3' '$4'
 	"
 #    else
@@ -29,13 +29,25 @@ match() {
     fi
 }
 
+imatch() {
+    if [ $1 = 1 ]; then
+	test_expect_success "iwildmatch:    match '$2' '$3'" "
+	    test-wildmatch iwildmatch '$2' '$3'
+	"
+    else
+	test_expect_success "iwildmatch: no match '$2' '$3'" "
+	    ! test-wildmatch iwildmatch '$2' '$3'
+	"
+    fi
+}
+
 pathmatch() {
     if [ $1 = 1 ]; then
-	test_expect_success "pathmatch:    match '$2' '$3'" "
+	test_expect_success "pathmatch:     match '$2' '$3'" "
 	    test-wildmatch pathmatch '$2' '$3'
 	"
     else
-	test_expect_success "pathmatch: no match '$2' '$3'" "
+	test_expect_success "pathmatch:  no match '$2' '$3'" "
 	    ! test-wildmatch pathmatch '$2' '$3'
 	"
     fi
@@ -235,4 +247,23 @@ pathmatch 1 abcXdefXghi '*X*i'
 pathmatch 1 ab/cXd/efXg/hi '*/*X*/*/*i'
 pathmatch 1 ab/cXd/efXg/hi '*Xg*i'
 
+# Case-sensitivy features
+match 0 x 'a' '[A-Z]'
+match 1 x 'A' '[A-Z]'
+match 0 x 'A' '[a-z]'
+match 1 x 'a' '[a-z]'
+match 0 x 'a' '[[:upper:]]'
+match 1 x 'A' '[[:upper:]]'
+match 0 x 'A' '[[:lower:]]'
+match 1 x 'a' '[[:lower:]]'
+
+imatch 1 'a' '[A-Z]'
+imatch 1 'A' '[A-Z]'
+imatch 1 'A' '[a-z]'
+imatch 1 'a' '[a-z]'
+imatch 1 'a' '[[:upper:]]'
+imatch 1 'A' '[[:upper:]]'
+imatch 1 'A' '[[:lower:]]'
+imatch 1 'a' '[[:lower:]]'
+
 test_done
wildmatch.c +7 -0
index 7192bdc..ea318d3 100644
@@ -196,6 +196,11 @@ 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)) {
+						t_ch = toupper(t_ch);
+						if (t_ch <= p_ch && t_ch >= prev_ch)
+							matched = 1;
+					}
 					p_ch = 0; /* This makes "prev_ch" get set to 0. */
 				} else if (p_ch == '[' && p[1] == ':') {
 					const uchar *s;
@@ -245,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					} else if (CC_EQ(s,i, "upper")) {
 						if (ISUPPER(t_ch))
 							matched = 1;
+						else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+							matched = 1;
 					} else if (CC_EQ(s,i, "xdigit")) {
 						if (ISXDIGIT(t_ch))
 							matched = 1;
Signature · 1 line
1.8.3
#1 lore raw

PATCH v2 1/1 wildmatch: properly fold case everywhere

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case fast.

That optimization is kept and ISLOWER() is used in the [:upper:] case when case folding is requested, while matching against a character range is retried with toupper() if the character was lowercase.

---
 t/t3070-wildmatch.sh | 47 +++++++++++++++++++++++++++++++++++++++++------
 wildmatch.c          |  7 +++++++
 2 files changed, 48 insertions(+), 6 deletions(-)
t/t3070-wildmatch.sh +41 -6
index 4c37057..e1b45e6 100755
@@ -6,20 +6,20 @@ test_description='wildmatch tests'
 
 match() {
     if [ $1 = 1 ]; then
-	test_expect_success "wildmatch:    match '$3' '$4'" "
+	test_expect_success "wildmatch:     match '$3' '$4'" "
 	    test-wildmatch wildmatch '$3' '$4'
 	"
     else
-	test_expect_success "wildmatch: no match '$3' '$4'" "
+	test_expect_success "wildmatch:  no match '$3' '$4'" "
 	    ! test-wildmatch wildmatch '$3' '$4'
 	"
     fi
     if [ $2 = 1 ]; then
-	test_expect_success "fnmatch:      match '$3' '$4'" "
+	test_expect_success "fnmatch:       match '$3' '$4'" "
 	    test-wildmatch fnmatch '$3' '$4'
 	"
     elif [ $2 = 0 ]; then
-	test_expect_success "fnmatch:   no match '$3' '$4'" "
+	test_expect_success "fnmatch:    no match '$3' '$4'" "
 	    ! test-wildmatch fnmatch '$3' '$4'
 	"
 #    else
@@ -29,13 +29,25 @@ match() {
     fi
 }
 
+imatch() {
+    if [ $1 = 1 ]; then
+	test_expect_success "iwildmatch:    match '$2' '$3'" "
+	    test-wildmatch iwildmatch '$2' '$3'
+	"
+    else
+	test_expect_success "iwildmatch: no match '$2' '$3'" "
+	    ! test-wildmatch iwildmatch '$2' '$3'
+	"
+    fi
+}
+
 pathmatch() {
     if [ $1 = 1 ]; then
-	test_expect_success "pathmatch:    match '$2' '$3'" "
+	test_expect_success "pathmatch:     match '$2' '$3'" "
 	    test-wildmatch pathmatch '$2' '$3'
 	"
     else
-	test_expect_success "pathmatch: no match '$2' '$3'" "
+	test_expect_success "pathmatch:  no match '$2' '$3'" "
 	    ! test-wildmatch pathmatch '$2' '$3'
 	"
     fi
@@ -235,4 +247,27 @@ pathmatch 1 abcXdefXghi '*X*i'
 pathmatch 1 ab/cXd/efXg/hi '*/*X*/*/*i'
 pathmatch 1 ab/cXd/efXg/hi '*Xg*i'
 
+# Case-sensitivy features
+match 0 x 'a' '[A-Z]'
+match 1 x 'A' '[A-Z]'
+match 0 x 'A' '[a-z]'
+match 1 x 'a' '[a-z]'
+match 0 x 'a' '[[:upper:]]'
+match 1 x 'A' '[[:upper:]]'
+match 0 x 'A' '[[:lower:]]'
+match 1 x 'a' '[[:lower:]]'
+match 0 x 'A' '[B-Za]'
+match 1 x 'a' '[B-Za]'
+
+imatch 1 'a' '[A-Z]'
+imatch 1 'A' '[A-Z]'
+imatch 1 'A' '[a-z]'
+imatch 1 'a' '[a-z]'
+imatch 1 'a' '[[:upper:]]'
+imatch 1 'A' '[[:upper:]]'
+imatch 1 'A' '[[:lower:]]'
+imatch 1 'a' '[[:lower:]]'
+imatch 1 'A' '[B-Za]'
+imatch 1 'a' '[B-Za]'
+
 test_done
wildmatch.c +7 -0
index 7192bdc..ea318d3 100644
@@ -196,6 +196,11 @@ 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)) {
+						t_ch = toupper(t_ch);
+						if (t_ch <= p_ch && t_ch >= prev_ch)
+							matched = 1;
+					}
 					p_ch = 0; /* This makes "prev_ch" get set to 0. */
 				} else if (p_ch == '[' && p[1] == ':') {
 					const uchar *s;
@@ -245,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					} else if (CC_EQ(s,i, "upper")) {
 						if (ISUPPER(t_ch))
 							matched = 1;
+						else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+							matched = 1;
 					} else if (CC_EQ(s,i, "xdigit")) {
 						if (ISXDIGIT(t_ch))
 							matched = 1;
Signature · 1 line
1.8.3
#2 lore raw

PATCH v3 1/1 wildmatch: properly fold case everywhere

In reply to Anthony Ramine

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case fast.

That optimization is kept and ISLOWER() is used in the [:upper:] case when case folding is requested, while matching against a character range is retried with toupper() if the character was lowercase.

---
 t/t3070-wildmatch.sh | 47 +++++++++++++++++++++++++++++++++++++++++------
 wildmatch.c          |  7 +++++++
 2 files changed, 48 insertions(+), 6 deletions(-)

Please disregard PATCH v2, it is identical to the first one.

t/t3070-wildmatch.sh +41 -6
index 4c37057..e1b45e6 100755
@@ -6,20 +6,20 @@ test_description='wildmatch tests'
 
 match() {
     if [ $1 = 1 ]; then
-	test_expect_success "wildmatch:    match '$3' '$4'" "
+	test_expect_success "wildmatch:     match '$3' '$4'" "
 	    test-wildmatch wildmatch '$3' '$4'
 	"
     else
-	test_expect_success "wildmatch: no match '$3' '$4'" "
+	test_expect_success "wildmatch:  no match '$3' '$4'" "
 	    ! test-wildmatch wildmatch '$3' '$4'
 	"
     fi
     if [ $2 = 1 ]; then
-	test_expect_success "fnmatch:      match '$3' '$4'" "
+	test_expect_success "fnmatch:       match '$3' '$4'" "
 	    test-wildmatch fnmatch '$3' '$4'
 	"
     elif [ $2 = 0 ]; then
-	test_expect_success "fnmatch:   no match '$3' '$4'" "
+	test_expect_success "fnmatch:    no match '$3' '$4'" "
 	    ! test-wildmatch fnmatch '$3' '$4'
 	"
 #    else
@@ -29,13 +29,25 @@ match() {
     fi
 }
 
+imatch() {
+    if [ $1 = 1 ]; then
+	test_expect_success "iwildmatch:    match '$2' '$3'" "
+	    test-wildmatch iwildmatch '$2' '$3'
+	"
+    else
+	test_expect_success "iwildmatch: no match '$2' '$3'" "
+	    ! test-wildmatch iwildmatch '$2' '$3'
+	"
+    fi
+}
+
 pathmatch() {
     if [ $1 = 1 ]; then
-	test_expect_success "pathmatch:    match '$2' '$3'" "
+	test_expect_success "pathmatch:     match '$2' '$3'" "
 	    test-wildmatch pathmatch '$2' '$3'
 	"
     else
-	test_expect_success "pathmatch: no match '$2' '$3'" "
+	test_expect_success "pathmatch:  no match '$2' '$3'" "
 	    ! test-wildmatch pathmatch '$2' '$3'
 	"
     fi
@@ -235,4 +247,27 @@ pathmatch 1 abcXdefXghi '*X*i'
 pathmatch 1 ab/cXd/efXg/hi '*/*X*/*/*i'
 pathmatch 1 ab/cXd/efXg/hi '*Xg*i'
 
+# Case-sensitivy features
+match 0 x 'a' '[A-Z]'
+match 1 x 'A' '[A-Z]'
+match 0 x 'A' '[a-z]'
+match 1 x 'a' '[a-z]'
+match 0 x 'a' '[[:upper:]]'
+match 1 x 'A' '[[:upper:]]'
+match 0 x 'A' '[[:lower:]]'
+match 1 x 'a' '[[:lower:]]'
+match 0 x 'A' '[B-Za]'
+match 1 x 'a' '[B-Za]'
+
+imatch 1 'a' '[A-Z]'
+imatch 1 'A' '[A-Z]'
+imatch 1 'A' '[a-z]'
+imatch 1 'a' '[a-z]'
+imatch 1 'a' '[[:upper:]]'
+imatch 1 'A' '[[:upper:]]'
+imatch 1 'A' '[[:lower:]]'
+imatch 1 'a' '[[:lower:]]'
+imatch 1 'A' '[B-Za]'
+imatch 1 'a' '[B-Za]'
+
 test_done
wildmatch.c +7 -0
index 7192bdc..f91ba99 100644
@@ -196,6 +196,11 @@ 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);
+						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;
@@ -245,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					} else if (CC_EQ(s,i, "upper")) {
 						if (ISUPPER(t_ch))
 							matched = 1;
+						else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+							matched = 1;
 					} else if (CC_EQ(s,i, "xdigit")) {
 						if (ISXDIGIT(t_ch))
 							matched = 1;
Signature · 1 line
1.8.3
#16 lore raw
In reply to Anthony Ramine

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 200

Quoted 6 lines · Anthony Ramine
wildmatch.c +2 -0
@@ -196,6 +196,11 @@ 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)) {
+                                               t_ch = toupper(t_ch);

This happens in a while loop where t_ch may be used again. Should we make a local copy of toupper(t_ch) and leave t_ch untouched?

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 206

Quoted 6 lines
wildmatch.c +3 -0
+                                               if (t_ch <= p_ch && t_ch >= prev_ch)
+                                                       matched = 1;
+                                       }
                                        p_ch = 0; /* This makes "prev_ch" get set to 0. */
                                } else if (p_ch == '[' && p[1] == ':') {
                                        const uchar *s;

--
Duy

#17 lore raw

You're right, I will amend my patch. How do I make git-send-email reply to that thread?

Signature · 1 line
Anthony Ramine
Quoted 19 lines · Duy Nguyen

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 200

Quoted 6 lines · Anthony Ramine
wildmatch.c +2 -0
@@ -196,6 +196,11 @@ 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)) {
+                                               t_ch = toupper(t_ch);

This happens in a while loop where t_ch may be used again. Should we make a local copy of toupper(t_ch) and leave t_ch untouched?

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 206

Quoted 6 lines
wildmatch.c +3 -0
+                                               if (t_ch <= p_ch && t_ch >= prev_ch)
+                                                       matched = 1;
+                                       }
                                       p_ch = 0; /* This makes "prev_ch" get set to 0. */
                               } else if (p_ch == '[' && p[1] == ':') {
                                       const uchar *s;

--
Duy

#3 lore raw
In reply to Anthony Ramine
Quoted 6 lines · Anthony Ramine

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case fast.

I did a little test with glibc fnmatch and also checked the source code. I don't think 'a' matches [:upper:]. So I'm not sure if that's a correct behavior or a bug in glibc. The spec is not clear (I think) on this. I guess we should just assume that 'a' should match '[:upper:]'?

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 203

Quoted 9 lines
wildmatch.c +5 -0
@@ -196,6 +196,11 @@ 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);
+                                               if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch)
+                                                       matched = 1;
+                                       }

Or we could stick with to tolower. Something like this

if ((t_ch <= p_ch && t_ch >= prev_ch) ||
((flags & WM_CASEFOLD) &&

      t_ch <= tolower(p_ch) && t_ch >= tolower(prev_ch)))

match = 1;

I think it's easier to read if we either downcase all, or upcase all, not both.

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 257

Quoted 12 lines
wildmatch.c +2 -0
                                        p_ch = 0; /* This makes "prev_ch" get set to 0. */
                                } else if (p_ch == '[' && p[1] == ':') {
                                        const uchar *s;
@@ -245,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
                                        } else if (CC_EQ(s,i, "upper")) {
                                                if (ISUPPER(t_ch))
                                                        matched = 1;
+                                               else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+                                                       matched = 1;
                                        } else if (CC_EQ(s,i, "xdigit")) {
                                                if (ISXDIGIT(t_ch))
                                                        matched = 1;

If WM_CASEFOLD is set, maybe isalpha(t_ch) is enough then? --
Duy

#4 lore raw

Replied inline.

Regards,

Signature · 1 line
Anthony Ramine
Quoted 8 lines · Duy Nguyen
Quoted 6 lines · Anthony Ramine

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case fast.

Quoted 4 lines

I did a little test with glibc fnmatch and also checked the source code. I don't think 'a' matches [:upper:]. So I'm not sure if that's a correct behavior or a bug in glibc. The spec is not clear (I think) on this. I guess we should just assume that 'a' should match '[:upper:]'?

I don't know, in my opinion if case folding is enabled we should say [:upper:], [:lower:] and [:alpha:] are equivalent.

This opinion is shared by GNU Flex [1]:

Quoted 1 line

• If your scanner is case-insensitive (the ‘-i’ flag), then ‘[:upper:]’ and ‘[:lower:]’ are equivalent to ‘[:alpha:]’.

[1] http://flex.sourceforge.net/manual/Patterns.html

Quoted 12 lines

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 203

Quoted 9 lines
wildmatch.c +5 -0
@@ -196,6 +196,11 @@ 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);
+                                               if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch)
+                                                       matched = 1;
+                                       }

Or we could stick with to tolower. Something like this

Quoted 6 lines

if ((t_ch <= p_ch && t_ch >= prev_ch) ||
((flags & WM_CASEFOLD) &&

     t_ch <= tolower(p_ch) && t_ch >= tolower(prev_ch)))

match = 1;

I think it's easier to read if we either downcase all, or upcase all, not both.

If the range to match against is [A-_], it will become [a-_] which is an empty range, ord('a') > ord('_'). I think it is simpler to reuse toupper() after the fact as I did.

Anyway maybe I should add a test for that corner case?

Quoted 6 lines
Quoted 6 lines
wildmatch.c +0 -0
                                       p_ch = 0; /* This makes "prev_ch" get set to 0. */
                               } else if (p_ch == '[' && p[1] == ':') {
                                       const uchar *s;
@@ -245,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
                                       } else if (CC_EQ(s,i, "upper")) {
                                               if (ISUPPER(t_ch))
Quoted 8 lines

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 257

Quoted 6 lines
wildmatch.c +2 -0
                                                       matched = 1;
+                                               else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+                                                       matched = 1;
                                       } else if (CC_EQ(s,i, "xdigit")) {
                                               if (ISXDIGIT(t_ch))
                                                       matched = 1;

If WM_CASEFOLD is set, maybe isalpha(t_ch) is enough then?

Yes isalpha() is enought but I wanted to keep the two cases separated, I can amend that if you want.

#5 lore raw
Quoted 15 lines · Anthony Ramine
Quoted 8 lines · Duy Nguyen
Quoted 6 lines · Anthony Ramine

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case fast.

Quoted 4 lines

I did a little test with glibc fnmatch and also checked the source code. I don't think 'a' matches [:upper:]. So I'm not sure if that's a correct behavior or a bug in glibc. The spec is not clear (I think) on this. I guess we should just assume that 'a' should match '[:upper:]'?

Quoted 7 lines

I don't know, in my opinion if case folding is enabled we should say [:upper:], [:lower:] and [:alpha:] are equivalent.

This opinion is shared by GNU Flex [1]:

Quoted 1 line

• If your scanner is case-insensitive (the ‘-i’ flag), then ‘[:upper:]’ and ‘[:lower:]’ are equivalent to ‘[:alpha:]’.

[1] http://flex.sourceforge.net/manual/Patterns.html

Then we should do it too because of this precedent, I think.

Quoted 17 lines
Quoted 17 lines

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 203

Quoted 9 lines
wildmatch.c +5 -0
@@ -196,6 +196,11 @@ 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);
+                                               if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch)
+                                                       matched = 1;
+                                       }

Or we could stick with to tolower. Something like this

if ((t_ch <= p_ch && t_ch >= prev_ch) ||
((flags & WM_CASEFOLD) &&

     t_ch <= tolower(p_ch) && t_ch >= tolower(prev_ch)))

match = 1;

Quoted 5 lines
Quoted 1 line

I think it's easier to read if we either downcase all, or upcase all, not both.

If the range to match against is [A-_], it will become [a-_] which is an empty range, ord('a') > ord('_'). I think it is simpler to reuse toupper() after the fact as I did.

Anyway maybe I should add a test for that corner case?

Yeah I was thinking about such a case, but I saw glibc do it... I guess we just found another bug, at least in compat/fnmatch.c. Yes a test for it would be great, in case I change my mind 2 years from now and decide to turn it the other way ;)

Quoted 16 lines
Quoted 15 lines

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 257

Quoted 13 lines
wildmatch.c +2 -0
                                       p_ch = 0; /* This makes "prev_ch" get set to 0. */
                               } else if (p_ch == '[' && p[1] == ':') {
                                       const uchar *s;
@@ -245,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
                                       } else if (CC_EQ(s,i, "upper")) {
                                               if (ISUPPER(t_ch))
                                                       matched = 1;
+                                               else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+                                                       matched = 1;
                                       } else if (CC_EQ(s,i, "xdigit")) {
                                               if (ISXDIGIT(t_ch))
                                                       matched = 1;

If WM_CASEFOLD is set, maybe isalpha(t_ch) is enough then?

Quoted 1 line

Yes isalpha() is enought but I wanted to keep the two cases separated, I can amend that if you want.

Either way is fine. I don't think this code is performance critical. Your call. --
Duy

#6 lore raw

Replied inline.

Signature · 1 line
Anthony Ramine
Quoted 45 lines · Duy Nguyen
Quoted 15 lines · Anthony Ramine
Quoted 8 lines · Duy Nguyen
Quoted 6 lines · Anthony Ramine

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case fast.

Quoted 4 lines

I did a little test with glibc fnmatch and also checked the source code. I don't think 'a' matches [:upper:]. So I'm not sure if that's a correct behavior or a bug in glibc. The spec is not clear (I think) on this. I guess we should just assume that 'a' should match '[:upper:]'?

Quoted 7 lines

I don't know, in my opinion if case folding is enabled we should say [:upper:], [:lower:] and [:alpha:] are equivalent.

This opinion is shared by GNU Flex [1]:

Quoted 1 line

• If your scanner is case-insensitive (the ‘-i’ flag), then ‘[:upper:]’ and ‘[:lower:]’ are equivalent to ‘[:alpha:]’.

[1] http://flex.sourceforge.net/manual/Patterns.html

Then we should do it too because of this precedent, I think.

Quoted 19 lines
Quoted 18 lines

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 203

Quoted 9 lines
wildmatch.c +5 -0
@@ -196,6 +196,11 @@ 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);
+                                               if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch)
+                                                       matched = 1;
+                                       }

Or we could stick with to tolower. Something like this

if ((t_ch <= p_ch && t_ch >= prev_ch) ||
((flags & WM_CASEFOLD) &&

    t_ch <= tolower(p_ch) && t_ch >= tolower(prev_ch)))

match = 1;

I think it's easier to read if we either downcase all, or upcase all, not both.

Quoted 8 lines
Quoted 3 lines

If the range to match against is [A-_], it will become [a-_] which is an empty range, ord('a') > ord('_'). I think it is simpler to reuse toupper() after the fact as I did.

Anyway maybe I should add a test for that corner case?

Yeah I was thinking about such a case, but I saw glibc do it... I guess we just found another bug, at least in compat/fnmatch.c. Yes a test for it would be great, in case I change my mind 2 years from now and decide to turn it the other way ;)

Should I patch compat/fnmatch.c too? That would make it different from the glibc's one.

Quoted 21 lines
Quoted 16 lines
Quoted 15 lines

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 257

Quoted 13 lines
wildmatch.c +2 -0
                                      p_ch = 0; /* This makes "prev_ch" get set to 0. */
                              } else if (p_ch == '[' && p[1] == ':') {
                                      const uchar *s;
@@ -245,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
                                      } else if (CC_EQ(s,i, "upper")) {
                                              if (ISUPPER(t_ch))
                                                      matched = 1;
+                                               else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+                                                       matched = 1;
                                      } else if (CC_EQ(s,i, "xdigit")) {
                                              if (ISXDIGIT(t_ch))
                                                      matched = 1;

If WM_CASEFOLD is set, maybe isalpha(t_ch) is enough then?

Quoted 1 line

Yes isalpha() is enought but I wanted to keep the two cases separated, I can amend that if you want.

Either way is fine. I don't think this code is performance critical. Your call. --
Duy

#7 lore raw
Quoted 10 lines · Anthony Ramine
Quoted 8 lines
Quoted 3 lines

If the range to match against is [A-_], it will become [a-_] which is an empty range, ord('a') > ord('_'). I think it is simpler to reuse toupper() after the fact as I did.

Anyway maybe I should add a test for that corner case?

Yeah I was thinking about such a case, but I saw glibc do it... I guess we just found another bug, at least in compat/fnmatch.c. Yes a test for it would be great, in case I change my mind 2 years from now and decide to turn it the other way ;)

Should I patch compat/fnmatch.c too? That would make it different from the glibc's one.

No. I plan to remove compat/fnmatch and always use wildmatch, even ignoring system's fnmatch. That would keep the matching behavior consistent across platforms.
--
Duy

#8 lore raw

PATCH 1/1 wildmatch: properly fold case everywhere

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case fast.

That optimization is kept and ISLOWER() is used in the [:upper:] case when case folding is requested, while matching against a character range is retried with toupper() if the character was lowercase, as the bounds of the range itself cannot be modified (in a case-insensitive context, [A-_] is not equivalent to [a-_]).

---
 t/t3070-wildmatch.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++------
 wildmatch.c          |  7 +++++++
 2 files changed, 56 insertions(+), 6 deletions(-)

I added four tests for the [A-_] range case and a note about it in the commit message.

t/t3070-wildmatch.sh +49 -6
index 4c37057..38446a0 100755
@@ -6,20 +6,20 @@ test_description='wildmatch tests'
 
 match() {
     if [ $1 = 1 ]; then
-	test_expect_success "wildmatch:    match '$3' '$4'" "
+	test_expect_success "wildmatch:     match '$3' '$4'" "
 	    test-wildmatch wildmatch '$3' '$4'
 	"
     else
-	test_expect_success "wildmatch: no match '$3' '$4'" "
+	test_expect_success "wildmatch:  no match '$3' '$4'" "
 	    ! test-wildmatch wildmatch '$3' '$4'
 	"
     fi
     if [ $2 = 1 ]; then
-	test_expect_success "fnmatch:      match '$3' '$4'" "
+	test_expect_success "fnmatch:       match '$3' '$4'" "
 	    test-wildmatch fnmatch '$3' '$4'
 	"
     elif [ $2 = 0 ]; then
-	test_expect_success "fnmatch:   no match '$3' '$4'" "
+	test_expect_success "fnmatch:    no match '$3' '$4'" "
 	    ! test-wildmatch fnmatch '$3' '$4'
 	"
 #    else
@@ -29,13 +29,25 @@ match() {
     fi
 }
 
+imatch() {
+    if [ $1 = 1 ]; then
+	test_expect_success "iwildmatch:    match '$2' '$3'" "
+	    test-wildmatch iwildmatch '$2' '$3'
+	"
+    else
+	test_expect_success "iwildmatch: no match '$2' '$3'" "
+	    ! test-wildmatch iwildmatch '$2' '$3'
+	"
+    fi
+}
+
 pathmatch() {
     if [ $1 = 1 ]; then
-	test_expect_success "pathmatch:    match '$2' '$3'" "
+	test_expect_success "pathmatch:     match '$2' '$3'" "
 	    test-wildmatch pathmatch '$2' '$3'
 	"
     else
-	test_expect_success "pathmatch: no match '$2' '$3'" "
+	test_expect_success "pathmatch:  no match '$2' '$3'" "
 	    ! test-wildmatch pathmatch '$2' '$3'
 	"
     fi
@@ -235,4 +247,35 @@ pathmatch 1 abcXdefXghi '*X*i'
 pathmatch 1 ab/cXd/efXg/hi '*/*X*/*/*i'
 pathmatch 1 ab/cXd/efXg/hi '*Xg*i'
 
+# Case-sensitivy features
+match 0 x 'a' '[A-Z]'
+match 1 x 'A' '[A-Z]'
+match 0 x 'A' '[a-z]'
+match 1 x 'a' '[a-z]'
+match 0 x 'a' '[[:upper:]]'
+match 1 x 'A' '[[:upper:]]'
+match 0 x 'A' '[[:lower:]]'
+match 1 x 'a' '[[:lower:]]'
+match 0 x 'A' '[B-Za]'
+match 1 x 'a' '[B-Za]'
+match 0 x 'A' '[B-a]'
+match 1 x 'a' '[B-a]'
+match 0 x 'z' '[Z-y]'
+match 1 x 'Z' '[Z-y]'
+
+imatch 1 'a' '[A-Z]'
+imatch 1 'A' '[A-Z]'
+imatch 1 'A' '[a-z]'
+imatch 1 'a' '[a-z]'
+imatch 1 'a' '[[:upper:]]'
+imatch 1 'A' '[[:upper:]]'
+imatch 1 'A' '[[:lower:]]'
+imatch 1 'a' '[[:lower:]]'
+imatch 1 'A' '[B-Za]'
+imatch 1 'a' '[B-Za]'
+imatch 1 'A' '[B-a]'
+imatch 1 'a' '[B-a]'
+imatch 1 'z' '[Z-y]'
+imatch 1 'Z' '[Z-y]'
+
 test_done
wildmatch.c +7 -0
index 7192bdc..f91ba99 100644
@@ -196,6 +196,11 @@ 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);
+						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;
@@ -245,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					} else if (CC_EQ(s,i, "upper")) {
 						if (ISUPPER(t_ch))
 							matched = 1;
+						else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+							matched = 1;
 					} else if (CC_EQ(s,i, "xdigit")) {
 						if (ISXDIGIT(t_ch))
 							matched = 1;
Signature · 1 line
1.8.3
#9 lore raw

PATCH v5 1/1 wildmatch: properly fold case everywhere

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string are preemptively lowercased to handle the base case fast.

That optimization is kept and ISLOWER() is used in the [:upper:] case when case folding is requested, while matching against a character range is retried with toupper() if the character was lowercase, as the bounds of the range itself cannot be modified (in a case-insensitive context, [A-_] is not equivalent to [a-_]).

---
 t/t3070-wildmatch.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++------
 wildmatch.c          |  7 +++++++
 2 files changed, 56 insertions(+), 6 deletions(-)

I added Duy as reviewer and fixed a typo in the commit message reported by Eric Sunshine.

t/t3070-wildmatch.sh +49 -6
index 4c37057..38446a0 100755
@@ -6,20 +6,20 @@ test_description='wildmatch tests'
 
 match() {
     if [ $1 = 1 ]; then
-	test_expect_success "wildmatch:    match '$3' '$4'" "
+	test_expect_success "wildmatch:     match '$3' '$4'" "
 	    test-wildmatch wildmatch '$3' '$4'
 	"
     else
-	test_expect_success "wildmatch: no match '$3' '$4'" "
+	test_expect_success "wildmatch:  no match '$3' '$4'" "
 	    ! test-wildmatch wildmatch '$3' '$4'
 	"
     fi
     if [ $2 = 1 ]; then
-	test_expect_success "fnmatch:      match '$3' '$4'" "
+	test_expect_success "fnmatch:       match '$3' '$4'" "
 	    test-wildmatch fnmatch '$3' '$4'
 	"
     elif [ $2 = 0 ]; then
-	test_expect_success "fnmatch:   no match '$3' '$4'" "
+	test_expect_success "fnmatch:    no match '$3' '$4'" "
 	    ! test-wildmatch fnmatch '$3' '$4'
 	"
 #    else
@@ -29,13 +29,25 @@ match() {
     fi
 }
 
+imatch() {
+    if [ $1 = 1 ]; then
+	test_expect_success "iwildmatch:    match '$2' '$3'" "
+	    test-wildmatch iwildmatch '$2' '$3'
+	"
+    else
+	test_expect_success "iwildmatch: no match '$2' '$3'" "
+	    ! test-wildmatch iwildmatch '$2' '$3'
+	"
+    fi
+}
+
 pathmatch() {
     if [ $1 = 1 ]; then
-	test_expect_success "pathmatch:    match '$2' '$3'" "
+	test_expect_success "pathmatch:     match '$2' '$3'" "
 	    test-wildmatch pathmatch '$2' '$3'
 	"
     else
-	test_expect_success "pathmatch: no match '$2' '$3'" "
+	test_expect_success "pathmatch:  no match '$2' '$3'" "
 	    ! test-wildmatch pathmatch '$2' '$3'
 	"
     fi
@@ -235,4 +247,35 @@ pathmatch 1 abcXdefXghi '*X*i'
 pathmatch 1 ab/cXd/efXg/hi '*/*X*/*/*i'
 pathmatch 1 ab/cXd/efXg/hi '*Xg*i'
 
+# Case-sensitivy features
+match 0 x 'a' '[A-Z]'
+match 1 x 'A' '[A-Z]'
+match 0 x 'A' '[a-z]'
+match 1 x 'a' '[a-z]'
+match 0 x 'a' '[[:upper:]]'
+match 1 x 'A' '[[:upper:]]'
+match 0 x 'A' '[[:lower:]]'
+match 1 x 'a' '[[:lower:]]'
+match 0 x 'A' '[B-Za]'
+match 1 x 'a' '[B-Za]'
+match 0 x 'A' '[B-a]'
+match 1 x 'a' '[B-a]'
+match 0 x 'z' '[Z-y]'
+match 1 x 'Z' '[Z-y]'
+
+imatch 1 'a' '[A-Z]'
+imatch 1 'A' '[A-Z]'
+imatch 1 'A' '[a-z]'
+imatch 1 'a' '[a-z]'
+imatch 1 'a' '[[:upper:]]'
+imatch 1 'A' '[[:upper:]]'
+imatch 1 'A' '[[:lower:]]'
+imatch 1 'a' '[[:lower:]]'
+imatch 1 'A' '[B-Za]'
+imatch 1 'a' '[B-Za]'
+imatch 1 'A' '[B-a]'
+imatch 1 'a' '[B-a]'
+imatch 1 'z' '[Z-y]'
+imatch 1 'Z' '[Z-y]'
+
 test_done
wildmatch.c +7 -0
index 7192bdc..f91ba99 100644
@@ -196,6 +196,11 @@ 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);
+						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;
@@ -245,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					} else if (CC_EQ(s,i, "upper")) {
 						if (ISUPPER(t_ch))
 							matched = 1;
+						else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+							matched = 1;
 					} else if (CC_EQ(s,i, "xdigit")) {
 						if (ISXDIGIT(t_ch))
 							matched = 1;
Signature · 1 line
1.8.3
Reviewed-by on v1 1/1
#12 lore raw
In reply to Anthony Ramine
Quoted 7 lines · Anthony Ramine

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case fast.

Quoted 7 lines

That optimization is kept and ISLOWER() is used in the [:upper:] case when case folding is requested, while matching against a character range is retried with toupper() if the character was lowercase, as the bounds of the range itself cannot be modified (in a case-insensitive context, [A-_] is not equivalent to [a-_]).

If you have time, you may want to send a similar patch to rsync, which contains original wildmatch implementation. It does not look much different from this one, except that (flags & WM_CASEFOLD) is replaced with force_lower_case. Thanks.
--
Duy

#13 lore raw
In reply to Anthony Ramine
Quoted 5 lines · Anthony Ramine

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case

Did you mean s/and preemptively/are preemptively/ ?

Quoted 9 lines

fast.

That optimization is kept and ISLOWER() is used in the [:upper:] case when case folding is requested, while matching against a character range is retried with toupper() if the character was lowercase, as the bounds of the range itself cannot be modified (in a case-insensitive context, [A-_] is not equivalent to [a-_]).

#14 lore raw

Yes indeed. Will amend. Should I add your name in Reviewed-by as well?

Signature · 1 line
Anthony Ramine
Quoted 18 lines · Eric Sunshine
Quoted 5 lines · Anthony Ramine

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case

Did you mean s/and preemptively/are preemptively/ ?

Quoted 9 lines

fast.

That optimization is kept and ISLOWER() is used in the [:upper:] case when case folding is requested, while matching against a character range is retried with toupper() if the character was lowercase, as the bounds of the range itself cannot be modified (in a case-insensitive context, [A-_] is not equivalent to [a-_]).

#15 lore raw
Quoted 1 line · Anthony Ramine

Yes indeed. Will amend. Should I add your name in Reviewed-by as well?

No. I merely spotted a minor typographical error.

Quoted 24 lines

--
Anthony Ramine

Quoted 18 lines · Eric Sunshine
Quoted 5 lines · Anthony Ramine

Case folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string and preemptively lowercased to handle the base case

Did you mean s/and preemptively/are preemptively/ ?

Quoted 9 lines

fast.

That optimization is kept and ISLOWER() is used in the [:upper:] case when case folding is requested, while matching against a character range is retried with toupper() if the character was lowercase, as the bounds of the range itself cannot be modified (in a case-insensitive context, [A-_] is not equivalent to [a-_]).

#10 lore raw
In reply to Anthony Ramine
Quoted 13 lines · Anthony Ramine

ase folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string are preemptively lowercased to handle the base case fast.

That optimization is kept and ISLOWER() is used in the [:upper:] case when case folding is requested, while matching against a character range is retried with toupper() if the character was lowercase, as the bounds of the range itself cannot be modified (in a case-insensitive context, [A-_] is not equivalent to [a-_]).

Quoted 3 lines

---

Thanks.

Quoted 26 lines
 t/t3070-wildmatch.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++------
 wildmatch.c          |  7 +++++++
 2 files changed, 56 insertions(+), 6 deletions(-)

I added Duy as reviewer and fixed a typo in the commit message reported by Eric Sunshine.

t/t3070-wildmatch.sh +2 -2
index 4c37057..38446a0 100755
@@ -6,20 +6,20 @@ test_description='wildmatch tests'
 
 match() {
     if [ $1 = 1 ]; then
-	test_expect_success "wildmatch:    match '$3' '$4'" "
+	test_expect_success "wildmatch:     match '$3' '$4'" "
 	    test-wildmatch wildmatch '$3' '$4'
 	"
     else
-	test_expect_success "wildmatch: no match '$3' '$4'" "
+	test_expect_success "wildmatch:  no match '$3' '$4'" "
 	    ! test-wildmatch wildmatch '$3' '$4'
 	"
     fi
     if [ $2 = 1 ]; then

t/t3070-wildmatch.sh test_description='wildmatch tests' · line 25

Quoted 10 lines
t/t3070-wildmatch.sh +2 -2
-	test_expect_success "fnmatch:      match '$3' '$4'" "
+	test_expect_success "fnmatch:       match '$3' '$4'" "
 	    test-wildmatch fnmatch '$3' '$4'
 	"
     elif [ $2 = 0 ]; then
-	test_expect_success "fnmatch:   no match '$3' '$4'" "
+	test_expect_success "fnmatch:    no match '$3' '$4'" "
 	    ! test-wildmatch fnmatch '$3' '$4'
 	"
 #    else

Is the above about aligning $3/$4 across checks of different types (i.e. purely cosmetic)? I am not complaining; just making sure if there is nothing deeper going on.

It is outside the scope of this change, but the shell style of this script (most notably use of [] instead of test) needs to be fixed someday, preferrably soon, including the commented out else clause at the end of the hunk.

Quoted 11 lines
t/t3070-wildmatch.sh +7 -0
@@ -235,4 +247,35 @@ pathmatch 1 abcXdefXghi '*X*i'
 pathmatch 1 ab/cXd/efXg/hi '*/*X*/*/*i'
 pathmatch 1 ab/cXd/efXg/hi '*Xg*i'
 
+# Case-sensitivy features
+match 0 x 'a' '[A-Z]'
+match 1 x 'A' '[A-Z]'
+match 0 x 'A' '[a-z]'
+match 1 x 'a' '[a-z]'
+match 0 x 'a' '[[:upper:]]'
+match 1 x 'A' '[[:upper:]]'

t/t3070-wildmatch.sh pathmatch 1 abcXdefXghi '*X*i' · line 266

Quoted 10 lines
t/t3070-wildmatch.sh +10 -0
+match 0 x 'A' '[[:lower:]]'
+match 1 x 'a' '[[:lower:]]'
+match 0 x 'A' '[B-Za]'
+match 1 x 'a' '[B-Za]'
+match 0 x 'A' '[B-a]'
+match 1 x 'a' '[B-a]'
+match 0 x 'z' '[Z-y]'
+match 1 x 'Z' '[Z-y]'
+
+imatch 1 'a' '[A-Z]'

Do we want "# Case-insensitivity features" commment here as well?

Quoted 21 lines
t/t3070-wildmatch.sh +14 -0
+imatch 1 'A' '[A-Z]'
+imatch 1 'A' '[a-z]'
+imatch 1 'a' '[a-z]'
+imatch 1 'a' '[[:upper:]]'
+imatch 1 'A' '[[:upper:]]'
+imatch 1 'A' '[[:lower:]]'
+imatch 1 'a' '[[:lower:]]'
+imatch 1 'A' '[B-Za]'
+imatch 1 'a' '[B-Za]'
+imatch 1 'A' '[B-a]'
+imatch 1 'a' '[B-a]'
+imatch 1 'z' '[Z-y]'
+imatch 1 'Z' '[Z-y]'
 
+

test_done

wildmatch.c +0 -0
index 7192bdc..f91ba99 100644

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 204

Quoted 9 lines
wildmatch.c +5 -0
@@ -196,6 +196,11 @@ 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);
+						if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch)
+							matched = 1;
+					}
 					p_ch = 0; /* This makes "prev_ch" get set to 0. */

Hmm, this looks somewhat strange.

* At the beginning of the outermost "per characters in the text" loop, we seem to downcase t_ch when WM_CASEFOLD is set.
* Also at the same place, we also seem to downcase p_ch under the same condition.

which makes me wonder why the fix is not like this:

	+	if (flags & WM_CASEFOLD) {
        +		if (ISUPPER(p_ch))
        +			p_ch = tolower(p_ch);
        +		if (prev_ch && ISUPPER(prev_ch))
        +			prev_ch = tolower(prev_ch);
	+	}
		if (t_ch <= p_ch && t_ch >= prev_ch)
			matched = 1;
		p_ch = 0; /* This sets "prev_ch" to 0 */

Ahh, OK, the "seemingly strange" construct is about handling a range like "[Z-y]"; we do not want to upcase or downcase the p_ch/prev_ch make the range "[z-y]" (empty) which would exclude bytes like "^", "_" or even "Z".

And it is also OK to downcase p_ch in a single-letter case, not the characters in a range, at the beginning of the outermost loop, because we always compare for equality against t_ch (which is downcased) in that case.

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 254

Quoted 6 lines
wildmatch.c +2 -0
@@ -245,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					} else if (CC_EQ(s,i, "upper")) {
 						if (ISUPPER(t_ch))
 							matched = 1;
+						else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+							matched = 1;

This also looks somewhat strange but correct in that t_ch is already downcased so we do not need a corresponding change for CC_EQ("lower") codepath.

Interesting. Will apply.

Thanks.

#11 lore raw

Hello Junio,

Replied inline.

Regards,

Signature · 1 line
Anthony Ramine
Quoted 53 lines · Junio C Hamano
Quoted 13 lines · Anthony Ramine

ase folding is not done correctly when matching against the [:upper:] character class and uppercased character ranges (e.g. A-Z). Specifically, an uppercase letter fails to match against any of them when case folding is requested because plain characters in the pattern and the whole string are preemptively lowercased to handle the base case fast.

That optimization is kept and ISLOWER() is used in the [:upper:] case when case folding is requested, while matching against a character range is retried with toupper() if the character was lowercase, as the bounds of the range itself cannot be modified (in a case-insensitive context, [A-_] is not equivalent to [a-_]).

Quoted 3 lines

---

Thanks.

Quoted 32 lines

t/t3070-wildmatch.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++------
wildmatch.c | 7 +++++++

2 files changed, 56 insertions(+), 6 deletions(-)

I added Duy as reviewer and fixed a typo in the commit message reported by Eric Sunshine.

t/t3070-wildmatch.sh +3 -4
index 4c37057..38446a0 100755
@@ -6,20 +6,20 @@ test_description='wildmatch tests'
 
 match() {
    if [ $1 = 1 ]; then
-	test_expect_success "wildmatch:    match '$3' '$4'" "
+	test_expect_success "wildmatch:     match '$3' '$4'" "
 	    test-wildmatch wildmatch '$3' '$4'
 	"
    else
-	test_expect_success "wildmatch: no match '$3' '$4'" "
+	test_expect_success "wildmatch:  no match '$3' '$4'" "
 	    ! test-wildmatch wildmatch '$3' '$4'
 	"
    fi
    if [ $2 = 1 ]; then
-	test_expect_success "fnmatch:      match '$3' '$4'" "
+	test_expect_success "fnmatch:       match '$3' '$4'" "
 	    test-wildmatch fnmatch '$3' '$4'
 	"
    elif [ $2 = 0 ]; then
-	test_expect_success "fnmatch:   no match '$3' '$4'" "
Quoted 8 lines

t/t3070-wildmatch.sh test_description='wildmatch tests' · line 25

Quoted 4 lines
t/t3070-wildmatch.sh +1 -0
+	test_expect_success "fnmatch:    no match '$3' '$4'" "
 	    ! test-wildmatch fnmatch '$3' '$4'
 	"
 #    else

Is the above about aligning $3/$4 across checks of different types (i.e. purely cosmetic)? I am not complaining; just making sure if there is nothing deeper going on.

Yes purely cosmetic.

Quoted 20 lines

It is outside the scope of this change, but the shell style of this script (most notably use of [] instead of test) needs to be fixed someday, preferrably soon, including the commented out else clause at the end of the hunk.

Quoted 15 lines
t/t3070-wildmatch.sh +0 -0
@@ -235,4 +247,35 @@ pathmatch 1 abcXdefXghi '*X*i'

pathmatch 1 ab/cXd/efXg/hi '*/*X*/*/*i'
pathmatch 1 ab/cXd/efXg/hi '*Xg*i'

t/t3070-wildmatch.sh +11 -0
+# Case-sensitivy features
+match 0 x 'a' '[A-Z]'
+match 1 x 'A' '[A-Z]'
+match 0 x 'A' '[a-z]'
+match 1 x 'a' '[a-z]'
+match 0 x 'a' '[[:upper:]]'
+match 1 x 'A' '[[:upper:]]'
+match 0 x 'A' '[[:lower:]]'
+match 1 x 'a' '[[:lower:]]'
+match 0 x 'A' '[B-Za]'
+match 1 x 'a' '[B-Za]'
Quoted 8 lines

t/t3070-wildmatch.sh pathmatch 1 abcXdefXghi '*X*i' · line 266

Quoted 6 lines
t/t3070-wildmatch.sh +6 -0
+match 0 x 'A' '[B-a]'
+match 1 x 'a' '[B-a]'
+match 0 x 'z' '[Z-y]'
+match 1 x 'Z' '[Z-y]'
+
+imatch 1 'a' '[A-Z]'

Do we want "# Case-insensitivity features" commment here as well?

I don't particularly care, some sections have titles, some don't.

Quoted 56 lines
Quoted 21 lines
t/t3070-wildmatch.sh +14 -0
+imatch 1 'A' '[A-Z]'
+imatch 1 'A' '[a-z]'
+imatch 1 'a' '[a-z]'
+imatch 1 'a' '[[:upper:]]'
+imatch 1 'A' '[[:upper:]]'
+imatch 1 'A' '[[:lower:]]'
+imatch 1 'a' '[[:lower:]]'
+imatch 1 'A' '[B-Za]'
+imatch 1 'a' '[B-Za]'
+imatch 1 'A' '[B-a]'
+imatch 1 'a' '[B-a]'
+imatch 1 'z' '[Z-y]'
+imatch 1 'Z' '[Z-y]'
 
+

test_done

wildmatch.c +0 -0
index 7192bdc..f91ba99 100644

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 204

Quoted 9 lines
wildmatch.c +5 -0
@@ -196,6 +196,11 @@ 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);
+						if (t_ch_upper <= p_ch && t_ch_upper >= prev_ch)
+							matched = 1;
+					}
 					p_ch = 0; /* This makes "prev_ch" get set to 0. */

Hmm, this looks somewhat strange.

* At the beginning of the outermost "per characters in the text" loop, we seem to downcase t_ch when WM_CASEFOLD is set.
* Also at the same place, we also seem to downcase p_ch under the same condition.

which makes me wonder why the fix is not like this:

	+	if (flags & WM_CASEFOLD) {
       +		if (ISUPPER(p_ch))
       +			p_ch = tolower(p_ch);
       +		if (prev_ch && ISUPPER(prev_ch))
       +			prev_ch = tolower(prev_ch);
	+	}
		if (t_ch <= p_ch && t_ch >= prev_ch)
			matched = 1;
		p_ch = 0; /* This sets "prev_ch" to 0 */

Ahh, OK, the "seemingly strange" construct is about handling a range like "[Z-y]"; we do not want to upcase or downcase the p_ch/prev_ch make the range "[z-y]" (empty) which would exclude bytes like "^", "_" or even "Z".

Quoted 4 lines

And it is also OK to downcase p_ch in a single-letter case, not the characters in a range, at the beginning of the outermost loop, because we always compare for equality against t_ch (which is downcased) in that case.

Yeah I tried to explain that in the commit message but it is surely too dense.

Quoted 10 lines

wildmatch.c static int dowild(const uchar *p, const uchar *text, unsigned int flags) · line 254

Quoted 6 lines
wildmatch.c +2 -0
@@ -245,6 +250,8 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					} else if (CC_EQ(s,i, "upper")) {
 						if (ISUPPER(t_ch))
 							matched = 1;
+						else if ((flags & WM_CASEFOLD) && ISLOWER(t_ch))
+							matched = 1;

This also looks somewhat strange but correct in that t_ch is already downcased so we do not need a corresponding change for CC_EQ("lower") codepath.

I chose to still lowercase everything first, to keep the common path as is.

Quoted 3 lines

Interesting. Will apply.

Thanks.

Great. You're welcome.