Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ glob.iml
*.dot
*.png
*.svg
patterns.txt
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: go
go:
- "1.7.X"
- "1.8.X"
- "1.9.X"
- "1.10.X"
- master
- 1.7.x
- 1.8.x
- 1.9.x
- 1.x

matrix:
allow_failures:
Expand Down
14 changes: 10 additions & 4 deletions bench.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#! /bin/bash

rnd=$(head -c4 </dev/urandom|xxd -p)

bench() {
filename="/tmp/$1-$2.bench"
local exp=".*"
if [[ ! -z $2 ]]; then
$exp = $2
fi
filename=$(echo "$rnd-$1.bench" | tr "/" "_")
if test -e "${filename}";
then
echo "Already exists ${filename}"
else
backup=`git rev-parse --abbrev-ref HEAD`
git checkout $1
git checkout "$1"
echo -n "Creating ${filename}... "
go test ./... -run=NONE -bench=$2 > "${filename}" -benchmem
go test ./... -run=NONE -bench="$exp" > "${filename}" -benchmem
echo "OK"
git checkout ${backup}
sleep 5
Expand All @@ -23,4 +29,4 @@ current=`git rev-parse --abbrev-ref HEAD`
bench ${to} $2
bench ${current} $2

benchcmp $3 "/tmp/${to}-$2.bench" "/tmp/${current}-$2.bench"
benchcmp $3 "$rnd-${to}.bench" "$rnd-${current}.bench"
106 changes: 89 additions & 17 deletions cmd/globdraw/main.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,116 @@
package main

import (
"bufio"
"flag"
"fmt"
"github.com/gobwas/glob"
"github.com/gobwas/glob/match"
"github.com/gobwas/glob/match/debug"
"os"
"os/exec"
"strings"
"unicode/utf8"

"github.com/gobwas/glob"
"github.com/gobwas/glob/match"
)

func main() {
pattern := flag.String("p", "", "pattern to draw")
sep := flag.String("s", "", "comma separated list of separators characters")
var (
pattern = flag.String("p", "", "pattern to draw")
sep = flag.String("s", "", "comma separated list of separators characters")
filepath = flag.String("file", "", "path for patterns file")
auto = flag.Bool("auto", false, "autoopen result")
offset = flag.Int("offset", 0, "patterns to skip")
)
flag.Parse()

if *pattern == "" {
flag.Usage()
os.Exit(1)
var patterns []string
if *pattern != "" {
patterns = append(patterns, *pattern)
}
if *filepath != "" {
file, err := os.Open(*filepath)
if err != nil {
fmt.Printf("could not open file: %v\n", err)
os.Exit(1)
}
s := bufio.NewScanner(file)
for s.Scan() {
fmt.Println(*offset)
if *offset > 0 {
*offset--
fmt.Println("skipped")
continue
}
patterns = append(patterns, s.Text())
}
file.Close()
}
if len(patterns) == 0 {
return
}

var separators []rune
if len(*sep) > 0 {
for _, c := range strings.Split(*sep, ",") {
if r, w := utf8.DecodeRuneInString(c); len(c) > w {
fmt.Println("only single charactered separators are allowed")
r, w := utf8.DecodeRuneInString(c)
if len(c) > w {
fmt.Printf("only single charactered separators are allowed: %+q\n", c)
os.Exit(1)
}
separators = append(separators, r)
}
}

br := bufio.NewReader(os.Stdin)
for _, p := range patterns {
g, err := glob.Compile(p, separators...)
if err != nil {
fmt.Printf("could not compile pattern %+q: %v\n", p, err)
os.Exit(1)
}
s := match.Graphviz(p, g.(match.Matcher))
if *auto {
fmt.Fprintf(os.Stdout, "pattern: %+q: ", p)
if err := open(s); err != nil {
fmt.Printf("could not open graphviz: %v", err)
os.Exit(1)
} else {
separators = append(separators, r)
}
if !next(br) {
return
}
} else {
fmt.Fprintln(os.Stdout, s)
}
}
}

glob, err := glob.Compile(*pattern, separators...)
func open(s string) error {
file, err := os.Create("glob.graphviz.png")
if err != nil {
fmt.Println("could not compile pattern:", err)
os.Exit(1)
return err
}
defer file.Close()
cmd := exec.Command("dot", "-Tpng")
cmd.Stdin = strings.NewReader(s)
cmd.Stdout = file
if err := cmd.Run(); err != nil {
return err
}
if err := file.Sync(); err != nil {
return err
}
cmd = exec.Command("open", file.Name())
return cmd.Run()
}

matcher := glob.(match.Matcher)
fmt.Fprint(os.Stdout, debug.Graphviz(*pattern, matcher))
func next(in *bufio.Reader) bool {
fmt.Fprint(os.Stdout, "cancel? [Y/n]: ")
p, err := in.ReadBytes('\n')
if err != nil {
return false
}
if p[0] == 'Y' {
return false
}
return true
}
3 changes: 2 additions & 1 deletion cmd/globtest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package main
import (
"flag"
"fmt"
"github.com/gobwas/glob"
"os"
"strings"
"testing"
"unicode/utf8"

"github.com/gobwas/glob"
)

func benchString(r testing.BenchmarkResult) string {
Expand Down
Loading