forked from mirror/glob
fixes
This commit is contained in:
parent
2cd1592c0b
commit
5abd72c544
|
@ -1,64 +1,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gobwas/glob"
|
"github.com/gobwas/glob"
|
||||||
"github.com/gobwas/glob/match"
|
"github.com/gobwas/glob/match"
|
||||||
"math/rand"
|
"github.com/gobwas/glob/match/debug"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
func draw(pattern string, m match.Matcher) string {
|
|
||||||
return fmt.Sprintf(`digraph G {graph[label="%s"];%s}`, pattern, graphviz(m, fmt.Sprintf("%x", rand.Int63())))
|
|
||||||
}
|
|
||||||
|
|
||||||
func graphviz(m match.Matcher, id string) string {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
|
|
||||||
switch matcher := m.(type) {
|
|
||||||
case match.BTree:
|
|
||||||
fmt.Fprintf(buf, `"%s"[label="%s"];`, id, matcher.Value.String())
|
|
||||||
for _, m := range []match.Matcher{matcher.Left, matcher.Right} {
|
|
||||||
switch n := m.(type) {
|
|
||||||
case nil:
|
|
||||||
rnd := rand.Int63()
|
|
||||||
fmt.Fprintf(buf, `"%x"[label="<nil>"];`, rnd)
|
|
||||||
fmt.Fprintf(buf, `"%s"->"%x";`, id, rnd)
|
|
||||||
|
|
||||||
default:
|
|
||||||
sub := fmt.Sprintf("%x", rand.Int63())
|
|
||||||
fmt.Fprintf(buf, `"%s"->"%s";`, id, sub)
|
|
||||||
fmt.Fprintf(buf, graphviz(n, sub))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case match.AnyOf:
|
|
||||||
fmt.Fprintf(buf, `"%s"[label="AnyOf"];`, id)
|
|
||||||
for _, m := range matcher.Matchers {
|
|
||||||
rnd := rand.Int63()
|
|
||||||
fmt.Fprintf(buf, graphviz(m, fmt.Sprintf("%x", rnd)))
|
|
||||||
fmt.Fprintf(buf, `"%s"->"%x";`, id, rnd)
|
|
||||||
}
|
|
||||||
|
|
||||||
case match.EveryOf:
|
|
||||||
fmt.Fprintf(buf, `"%s"[label="EveryOf"];`, id)
|
|
||||||
for _, m := range matcher.Matchers {
|
|
||||||
rnd := rand.Int63()
|
|
||||||
fmt.Fprintf(buf, graphviz(m, fmt.Sprintf("%x", rnd)))
|
|
||||||
fmt.Fprintf(buf, `"%s"->"%x";`, id, rnd)
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
fmt.Fprintf(buf, `"%s"[label="%s"];`, id, m.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
return buf.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pattern := flag.String("p", "", "pattern to draw")
|
pattern := flag.String("p", "", "pattern to draw")
|
||||||
sep := flag.String("s", "", "comma separated list of separators characters")
|
sep := flag.String("s", "", "comma separated list of separators characters")
|
||||||
|
@ -70,12 +22,14 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var separators []rune
|
var separators []rune
|
||||||
for _, c := range strings.Split(*sep, ",") {
|
if len(*sep) > 0 {
|
||||||
if r, w := utf8.DecodeRuneInString(c); len(c) > w {
|
for _, c := range strings.Split(*sep, ",") {
|
||||||
fmt.Println("only single charactered separators are allowed")
|
if r, w := utf8.DecodeRuneInString(c); len(c) > w {
|
||||||
os.Exit(1)
|
fmt.Println("only single charactered separators are allowed")
|
||||||
} else {
|
os.Exit(1)
|
||||||
separators = append(separators, r)
|
} else {
|
||||||
|
separators = append(separators, r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,5 +40,5 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
matcher := glob.(match.Matcher)
|
matcher := glob.(match.Matcher)
|
||||||
fmt.Fprint(os.Stdout, draw(*pattern, matcher))
|
fmt.Fprint(os.Stdout, debug.Graphviz(*pattern, matcher))
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
package debug
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"github.com/gobwas/glob/match"
|
||||||
|
"math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Graphviz(pattern string, m match.Matcher) string {
|
||||||
|
return fmt.Sprintf(`digraph G {graph[label="%s"];%s}`, pattern, graphviz_internal(m, fmt.Sprintf("%x", rand.Int63())))
|
||||||
|
}
|
||||||
|
|
||||||
|
func graphviz_internal(m match.Matcher, id string) string {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
|
switch matcher := m.(type) {
|
||||||
|
case match.BTree:
|
||||||
|
fmt.Fprintf(buf, `"%s"[label="%s"];`, id, matcher.Value.String())
|
||||||
|
for _, m := range []match.Matcher{matcher.Left, matcher.Right} {
|
||||||
|
switch n := m.(type) {
|
||||||
|
case nil:
|
||||||
|
rnd := rand.Int63()
|
||||||
|
fmt.Fprintf(buf, `"%x"[label="<nil>"];`, rnd)
|
||||||
|
fmt.Fprintf(buf, `"%s"->"%x";`, id, rnd)
|
||||||
|
|
||||||
|
default:
|
||||||
|
sub := fmt.Sprintf("%x", rand.Int63())
|
||||||
|
fmt.Fprintf(buf, `"%s"->"%s";`, id, sub)
|
||||||
|
fmt.Fprintf(buf, graphviz_internal(n, sub))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case match.AnyOf:
|
||||||
|
fmt.Fprintf(buf, `"%s"[label="AnyOf"];`, id)
|
||||||
|
for _, m := range matcher.Matchers {
|
||||||
|
rnd := rand.Int63()
|
||||||
|
fmt.Fprintf(buf, graphviz_internal(m, fmt.Sprintf("%x", rnd)))
|
||||||
|
fmt.Fprintf(buf, `"%s"->"%x";`, id, rnd)
|
||||||
|
}
|
||||||
|
|
||||||
|
case match.EveryOf:
|
||||||
|
fmt.Fprintf(buf, `"%s"[label="EveryOf"];`, id)
|
||||||
|
for _, m := range matcher.Matchers {
|
||||||
|
rnd := rand.Int63()
|
||||||
|
fmt.Fprintf(buf, graphviz_internal(m, fmt.Sprintf("%x", rnd)))
|
||||||
|
fmt.Fprintf(buf, `"%s"->"%x";`, id, rnd)
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
fmt.Fprintf(buf, `"%s"[label="%s"];`, id, m.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.String()
|
||||||
|
}
|
|
@ -26,13 +26,13 @@ func main() {
|
||||||
g.Match("api.github.com") // true
|
g.Match("api.github.com") // true
|
||||||
|
|
||||||
// create new glob with set of delimiters as ["."]
|
// create new glob with set of delimiters as ["."]
|
||||||
g = glob.MustCompile("api.*.com", ".")
|
g = glob.MustCompile("api.*.com", '.')
|
||||||
g.Match("api.github.com") // true
|
g.Match("api.github.com") // true
|
||||||
g.Match("api.gi.hub.com") // false
|
g.Match("api.gi.hub.com") // false
|
||||||
|
|
||||||
// create new glob with set of delimiters as ["."]
|
// create new glob with set of delimiters as ["."]
|
||||||
// but now with super wildcard
|
// but now with super wildcard
|
||||||
g = glob.MustCompile("api.**.com", ".")
|
g = glob.MustCompile("api.**.com", '.')
|
||||||
g.Match("api.github.com") // true
|
g.Match("api.github.com") // true
|
||||||
g.Match("api.gi.hub.com") // true
|
g.Match("api.gi.hub.com") // true
|
||||||
|
|
||||||
|
@ -42,8 +42,8 @@ func main() {
|
||||||
g.Match("fat") // true
|
g.Match("fat") // true
|
||||||
g.Match("at") // false
|
g.Match("at") // false
|
||||||
|
|
||||||
// create glob with single symbol wildcard and delimiters ["f"]
|
// create glob with single symbol wildcard and delimiters ['f']
|
||||||
g = glob.MustCompile("?at", "f")
|
g = glob.MustCompile("?at", 'f')
|
||||||
g.Match("cat") // true
|
g.Match("cat") // true
|
||||||
g.Match("fat") // false
|
g.Match("fat") // false
|
||||||
g.Match("at") // false
|
g.Match("at") // false
|
||||||
|
|
Loading…
Reference in New Issue