From 1c8ecd3831410f1bbbdd7347fd34ac300e64c987 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sat, 29 Jul 2023 16:43:22 -0700 Subject: [PATCH] d2ir: Make globs case insensitive to match the rest of the language note: I personally wish to change the language and make it case sensitive. --- d2ir/pattern.go | 2 +- d2ir/pattern_test.go | 12 ++ .../d2ir/TestCompile/patterns/case.exp.json | 159 ++++++++++++++++++ 3 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 testdata/d2ir/TestCompile/patterns/case.exp.json diff --git a/d2ir/pattern.go b/d2ir/pattern.go index 90773cca3b..c2ccf237c3 100644 --- a/d2ir/pattern.go +++ b/d2ir/pattern.go @@ -44,7 +44,7 @@ func matchPattern(s string, pattern []string) bool { i++ } } else { - if !strings.HasPrefix(s, pattern[i]) { + if !strings.HasPrefix(strings.ToLower(s), strings.ToLower(pattern[i])) { return false } s = s[len(pattern[i]):] diff --git a/d2ir/pattern_test.go b/d2ir/pattern_test.go index bae678f4a1..ff91934d82 100644 --- a/d2ir/pattern_test.go +++ b/d2ir/pattern_test.go @@ -35,6 +35,18 @@ a*: globbed`) assertQuery(t, m, 0, 0, "globbed", "action") }, }, + { + name: "case", + run: func(t testing.TB) { + m, err := compile(t, `animal: meow +action: yes +A*: globbed`) + assert.Success(t, err) + assertQuery(t, m, 2, 0, nil, "") + assertQuery(t, m, 0, 0, "globbed", "animal") + assertQuery(t, m, 0, 0, "globbed", "action") + }, + }, { name: "suffix", run: func(t testing.TB) { diff --git a/testdata/d2ir/TestCompile/patterns/case.exp.json b/testdata/d2ir/TestCompile/patterns/case.exp.json new file mode 100644 index 0000000000..4c0ba17656 --- /dev/null +++ b/testdata/d2ir/TestCompile/patterns/case.exp.json @@ -0,0 +1,159 @@ +{ + "fields": [ + { + "name": "animal", + "primary": { + "value": { + "range": "TestCompile/patterns/case.d2,2:4:29-2:11:36", + "value": [ + { + "string": "globbed", + "raw_string": "globbed" + } + ] + } + }, + "references": [ + { + "string": { + "range": "TestCompile/patterns/case.d2,0:0:0-0:6:6", + "value": [ + { + "string": "animal", + "raw_string": "animal" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/case.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/case.d2,0:0:0-0:6:6", + "value": [ + { + "string": "animal", + "raw_string": "animal" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "TestCompile/patterns/case.d2,0:0:0-0:12:12", + "key": { + "range": "TestCompile/patterns/case.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/case.d2,0:0:0-0:6:6", + "value": [ + { + "string": "animal", + "raw_string": "animal" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "TestCompile/patterns/case.d2,0:8:8-0:12:12", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + } + } + } + } + ] + }, + { + "name": "action", + "primary": { + "value": { + "range": "TestCompile/patterns/case.d2,2:4:29-2:11:36", + "value": [ + { + "string": "globbed", + "raw_string": "globbed" + } + ] + } + }, + "references": [ + { + "string": { + "range": "TestCompile/patterns/case.d2,1:0:13-1:6:19", + "value": [ + { + "string": "action", + "raw_string": "action" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/case.d2,1:0:13-1:6:19", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/case.d2,1:0:13-1:6:19", + "value": [ + { + "string": "action", + "raw_string": "action" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "TestCompile/patterns/case.d2,1:0:13-1:11:24", + "key": { + "range": "TestCompile/patterns/case.d2,1:0:13-1:6:19", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/case.d2,1:0:13-1:6:19", + "value": [ + { + "string": "action", + "raw_string": "action" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "TestCompile/patterns/case.d2,1:8:21-1:11:24", + "value": [ + { + "string": "yes", + "raw_string": "yes" + } + ] + } + } + } + } + } + ] + } + ], + "edges": null +}