Skip to content

Commit f5258e7

Browse files
author
Codehardt
committed
fix: fixed a bug that caused some strings to not match due to ignored 'or string'
1 parent 7c06fa5 commit f5258e7

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

parser_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func Example_parse() {
1919
p(`"foo" AND "bar"`)
2020
p(`"foo" AND NOT "bar"`)
2121
p(`"foo" AND NOT ("bar" OR "baz")`)
22+
p(`("foo" OR "bar") AND ("bar" OR "baz") AND ("baaz" OR "qux")`)
2223
// Output:
2324
// ----- "foo" -----
2425
// nodeVAL{"foo"}
@@ -28,6 +29,8 @@ func Example_parse() {
2829
// nodeAND{nodeVAL{"foo"},nodeNOT{nodeVAL{"bar"}}}
2930
// ----- "foo" AND NOT ("bar" OR "baz") -----
3031
// nodeAND{nodeVAL{"foo"},nodeNOT{nodeOR{nodeVAL{"bar"},nodeVAL{"baz"}}}}
32+
// ----- ("foo" OR "bar") AND ("bar" OR "baz") AND ("baaz" OR "qux") -----
33+
// nodeAND{nodeAND{nodeOR{nodeVAL{"foo"},nodeVAL{"bar"}},nodeOR{nodeVAL{"bar"},nodeVAL{"baz"}}},nodeOR{nodeVAL{"baaz"},nodeVAL{"qux"}}}
3134
}
3235

3336
func Example_parse_multi() {

sop.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,19 @@ func getAndPaths(n node) []andPath {
6262
func getUnsortedAndPaths(n node) []andPath {
6363
switch v := n.(type) {
6464
case nodeAND:
65-
return []andPath{append(getUnsortedAndPaths(v.node1)[0], getUnsortedAndPaths(v.node2)[0]...)}
65+
var res []andPath
66+
c1 := getUnsortedAndPaths(v.node1)
67+
c2 := getUnsortedAndPaths(v.node2)
68+
for _, andPathC1 := range c1 {
69+
for _, andPathC2 := range c2 {
70+
res = append(res, append(andPathC1, andPathC2...))
71+
}
72+
}
73+
return res
6674
case nodeOR:
67-
return append(getUnsortedAndPaths(v.node1), getUnsortedAndPaths(v.node2)...)
75+
c1 := getUnsortedAndPaths(v.node1)
76+
c2 := getUnsortedAndPaths(v.node2)
77+
return append(c1, c2...)
6878
case nodeNOT:
6979
val := v.node.(nodeVAL)
7080
return []andPath{

sop_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func ExampleSOP() {
104104
sop(`"a" AND "b"`)
105105
sop(`"a" OR "b"`)
106106
sop(`"a" AND ("b" OR "c")`)
107+
sop(`"a" AND ("b" OR "c") AND ("d" OR "e")`)
107108
sop(`"a" OR ("b" AND "c")`)
108109
sop(`"a" AND NOT ("b" OR "c")`)
109110
sop(`"a" OR NOT ("b" AND "c")`)
@@ -129,6 +130,9 @@ func ExampleSOP() {
129130
// ----- "a" AND ("b" OR "c") -----
130131
// before: ("a" AND ("b" OR "c"))
131132
// after: (("a" AND "b") OR ("a" AND "c"))
133+
// ----- "a" AND ("b" OR "c") AND ("d" OR "e") -----
134+
// before: (("a" AND ("b" OR "c")) AND ("d" OR "e"))
135+
//after: (((("a" AND "b") OR ("a" AND "c")) AND "d") OR ((("a" AND "b") OR ("a" AND "c")) AND "e"))
132136
// ----- "a" OR ("b" AND "c") -----
133137
// before: ("a" OR ("b" AND "c"))
134138
// after: ("a" OR ("b" AND "c"))
@@ -183,9 +187,15 @@ func ExampleMatchStrings() {
183187
fmt.Println(matchPath.String())
184188
}
185189
}
186-
ms(`("a" AND NOT "b") AND NOT (NOT "c" OR "d") AND ("f" OR "g")`)
190+
ms(`("a" OR "b") AND ("c" OR "d") AND ("e" OR "f")`)
187191
// Output:
188-
// ----- ("a" AND NOT "b") AND NOT (NOT "c" OR "d") AND ("f" OR "g") -----
189-
// "a", "c", "f", NOT "b", NOT "d"
190-
// "a", "c", "g", NOT "b", NOT "d"
192+
// ----- ("a" OR "b") AND ("c" OR "d") AND ("e" OR "f") -----
193+
// "a", "c", "e"
194+
// "a", "d", "e"
195+
// "b", "c", "e"
196+
// "b", "d", "e"
197+
// "a", "c", "f"
198+
// "a", "d", "f"
199+
// "b", "c", "f"
200+
// "b", "d", "f"
191201
}

0 commit comments

Comments
 (0)