diff --git a/compiler.go b/compiler.go index 3b79c95..e8aaf64 100644 --- a/compiler.go +++ b/compiler.go @@ -215,6 +215,10 @@ func convertMatchers(matchers []match.Matcher, result []match.Matcher) []match.M } func compileMatchers(matchers []match.Matcher) (match.Matcher, error) { + if len(matchers) == 0 { + return nil, fmt.Errorf("compile error: need at least one matcher") + } + if m := glueMatchers(matchers); m != nil { return m, nil } @@ -233,9 +237,8 @@ func compileMatchers(matchers []match.Matcher) (match.Matcher, error) { } } - if val == nil { - return nil, fmt.Errorf("could not convert matchers %s: need at least one matcher", match.Matchers(matchers)) - } + // _, ok := val.(match.BTree) + // fmt.Println("a tree", ok) left := matchers[:idx] var right []match.Matcher diff --git a/glob_test.go b/glob_test.go index df8e0ab..6233f08 100644 --- a/glob_test.go +++ b/glob_test.go @@ -1,7 +1,10 @@ package glob import ( + "bytes" + "fmt" "github.com/gobwas/glob/match" + "math/rand" "reflect" "testing" ) @@ -35,6 +38,43 @@ func glob(s bool, p, m string, d ...string) test { return test{p, m, s, d} } +func draw(pattern string, m match.Matcher) string { + if tree, ok := m.(match.BTree); ok { + return fmt.Sprintf(`digraph G {graph[label="%s"];%s}`, pattern, graphviz(tree, fmt.Sprintf("%x", rand.Int63()))) + } + + return m.String() +} + +func graphviz(tree match.BTree, id string) string { + buf := &bytes.Buffer{} + + fmt.Fprintf(buf, `"%s"[label="%s"];`, id, tree.Value.String()) + for _, m := range []match.Matcher{tree.Left, tree.Right} { + switch n := m.(type) { + case nil: + rnd := rand.Int63() + fmt.Fprintf(buf, `"%x"[label=""];`, rnd) + // fmt.Fprintf(buf, `"%s"->"%x"[label="len = 0"];`, id, rnd) + fmt.Fprintf(buf, `"%s"->"%x";`, id, rnd) + + case match.BTree: + sub := fmt.Sprintf("%x", rand.Int63()) + // fmt.Fprintf(buf, `"%s"->"%s"[label="len=%d"];`, id, sub, n.Len()) + fmt.Fprintf(buf, `"%s"->"%s";`, id, sub) + fmt.Fprintf(buf, graphviz(n, sub)) + + default: + rnd := rand.Int63() + fmt.Fprintf(buf, `"%x"[label="%s"];`, rnd, m.String()) + // fmt.Fprintf(buf, `"%s"->"%x"[label="len = %d"];`, id, rnd, m.Len()) + fmt.Fprintf(buf, `"%s"->"%x";`, id, rnd) + } + } + + return buf.String() +} + func TestCompilePattern(t *testing.T) { for id, test := range []struct { pattern string @@ -42,7 +82,11 @@ func TestCompilePattern(t *testing.T) { exp match.Matcher }{ { - pattern: "a?*", + pattern: "left*??B*abcd*[!b]??*abc*right", + exp: match.Raw{"t"}, + }, + { + pattern: "abc*??def", exp: match.Raw{"t"}, }, } { @@ -53,9 +97,8 @@ func TestCompilePattern(t *testing.T) { } matcher := glob.(match.Matcher) - if !reflect.DeepEqual(test.exp, matcher) { - t.Errorf("#%d unexpected compilation:\nexp: %s\nact: %s", id, test.exp, matcher) + t.Errorf("#%d unexpected compilation:\nexp: %s\nact: %s", id, test.exp, draw(test.pattern, matcher)) continue } } diff --git a/match/any.go b/match/any.go index 92c6274..f554b6a 100644 --- a/match/any.go +++ b/match/any.go @@ -44,5 +44,5 @@ func (self Any) Kind() Kind { } func (self Any) String() string { - return fmt.Sprintf("[any:%s]", self.Separators) + return fmt.Sprintf("", self.Separators) } diff --git a/match/any_of.go b/match/any_of.go index 605a771..46a6f36 100644 --- a/match/any_of.go +++ b/match/any_of.go @@ -84,5 +84,5 @@ func (self AnyOf) Kind() Kind { } func (self AnyOf) String() string { - return fmt.Sprintf("[any_of:%s]", self.Matchers) + return fmt.Sprintf("", self.Matchers) } diff --git a/match/btree.go b/match/btree.go index efa6ebc..9c3534d 100644 --- a/match/btree.go +++ b/match/btree.go @@ -112,23 +112,6 @@ func (self BTree) Match(s string) bool { return false } -const tpl = ` -"%p"[label="%s"] -"%p"[label="%s"] -"%p"[label="%s"] -"%p"->"%p" -"%p"->"%p" -` - func (self BTree) String() string { - // return fmt.Sprintf("[btree:%s<-%s->%s]", self.Left, self.Value, self.Right) - - l, r := "nil", "nil" - if self.Left != nil { - l = self.Left.String() - } - if self.Right != nil { - r = self.Right.String() - } - return fmt.Sprintf(tpl, &self, self.Value, &l, l, &r, r, &self, &l, &self, &r) + return fmt.Sprintf("%s]>", self.Left, self.Value, self.Right) } diff --git a/match/contains.go b/match/contains.go index 329dc3b..d7b2154 100644 --- a/match/contains.go +++ b/match/contains.go @@ -61,5 +61,9 @@ func (self Contains) Kind() Kind { } func (self Contains) String() string { - return fmt.Sprintf("[contains:needle=%s not=%t]", self.Needle, self.Not) + var not string + if self.Not { + not = "!" + } + return fmt.Sprintf("", not, self.Needle) } diff --git a/match/every_of.go b/match/every_of.go index 36af182..4174ce3 100644 --- a/match/every_of.go +++ b/match/every_of.go @@ -79,5 +79,5 @@ func (self EveryOf) Kind() Kind { } func (self EveryOf) String() string { - return fmt.Sprintf("[every_of:%s]", self.Matchers) + return fmt.Sprintf("", self.Matchers) } diff --git a/match/list.go b/match/list.go index e83edd1..2ff1e1f 100644 --- a/match/list.go +++ b/match/list.go @@ -40,5 +40,10 @@ func (self List) Index(s string) (int, []int) { } func (self List) String() string { - return fmt.Sprintf("[list:list=%s not=%t]", self.List, self.Not) + var not string + if self.Not { + not = "!" + } + + return fmt.Sprintf("", not, self.List) } diff --git a/match/match.go b/match/match.go index b22bc51..d16f9a9 100644 --- a/match/match.go +++ b/match/match.go @@ -42,7 +42,7 @@ func (m Matchers) String() string { s = append(s, fmt.Sprint(matcher)) } - return fmt.Sprintf("matchers[%s]", strings.Join(s, ",")) + return fmt.Sprintf("%s", strings.Join(s, ",")) } func appendIfNotAsPrevious(target []int, val int) []int { diff --git a/match/max.go b/match/max.go index 58fffb6..67e5378 100644 --- a/match/max.go +++ b/match/max.go @@ -46,5 +46,5 @@ func (self Max) Kind() Kind { } func (self Max) String() string { - return fmt.Sprintf("[max:%d]", self.Limit) + return fmt.Sprintf("", self.Limit) } diff --git a/match/min.go b/match/min.go index 071ef88..5bba994 100644 --- a/match/min.go +++ b/match/min.go @@ -45,5 +45,5 @@ func (self Min) Kind() Kind { } func (self Min) String() string { - return fmt.Sprintf("[min:%d]", self.Limit) + return fmt.Sprintf("", self.Limit) } diff --git a/match/prefix.go b/match/prefix.go index 67b8856..49b22e6 100644 --- a/match/prefix.go +++ b/match/prefix.go @@ -54,5 +54,5 @@ func (self Prefix) Match(s string) bool { } func (self Prefix) String() string { - return fmt.Sprintf("[prefix:%s]", self.Prefix) + return fmt.Sprintf("", self.Prefix) } diff --git a/match/prefix_suffix.go b/match/prefix_suffix.go index e2a4838..9efffea 100644 --- a/match/prefix_suffix.go +++ b/match/prefix_suffix.go @@ -60,5 +60,5 @@ func (self PrefixSuffix) Match(s string) bool { } func (self PrefixSuffix) String() string { - return fmt.Sprintf("[prefix_suffix:%s-%s]", self.Prefix, self.Suffix) + return fmt.Sprintf("", self.Prefix, self.Suffix) } diff --git a/match/range.go b/match/range.go index e709376..e771285 100644 --- a/match/range.go +++ b/match/range.go @@ -40,5 +40,9 @@ func (self Range) Index(s string) (int, []int) { } func (self Range) String() string { - return fmt.Sprintf("[range:%s-%s(%t)]", string(self.Lo), string(self.Hi), self.Not) + var not string + if self.Not { + not = "!" + } + return fmt.Sprintf("", not, string(self.Lo), string(self.Hi)) } diff --git a/match/row.go b/match/row.go index 46b4c90..2c337df 100644 --- a/match/row.go +++ b/match/row.go @@ -62,5 +62,5 @@ func (self Row) Kind() Kind { } func (self Row) String() string { - return fmt.Sprintf("[row:%s]", self.Matchers) + return fmt.Sprintf("", self.Matchers) } diff --git a/match/single.go b/match/single.go index f60d883..833f4c7 100644 --- a/match/single.go +++ b/match/single.go @@ -34,5 +34,5 @@ func (self Single) Kind() Kind { } func (self Single) String() string { - return fmt.Sprintf("[single:%s]", self.Separators) + return fmt.Sprintf("", self.Separators) } diff --git a/match/suffix.go b/match/suffix.go index 15266b7..c9e0425 100644 --- a/match/suffix.go +++ b/match/suffix.go @@ -39,5 +39,5 @@ func (self Suffix) Match(s string) bool { } func (self Suffix) String() string { - return fmt.Sprintf("[suffix:%s]", self.Suffix) + return fmt.Sprintf("", self.Suffix) } diff --git a/match/super.go b/match/super.go index f69bd8b..ad50d43 100644 --- a/match/super.go +++ b/match/super.go @@ -31,5 +31,5 @@ func (self Super) Kind() Kind { } func (self Super) String() string { - return fmt.Sprintf("[super]") + return fmt.Sprintf("") }